Pointers ignoring unsigned trait in C? -
i'm curious why following code returns negative numbers though every single variable , fuction initialization in program unsigned int. thought supposed positive?.
#include <stdio.h> unsigned int main () { unsigned int ch = 0; unsigned int asteriskvalue = 0; while(ch!=27) { ch = getch(); if (ch == 224) { increasedecrease(&asteriskvalue); } printf("%d",asteriskvalue); } } void increasedecrease(unsigned int *value) { unsigned int upordown; upordown = getch (); if(upordown == 72) (*value)--; else if(upordown == 80) (*value)++; }
i think results positive value, printing "%d" flag, tells printf interpret signed value, if casting on fly type: (signed)asteriskvalue
try printing "%u" formatting instead of "%d".
Comments
Post a Comment