Print wrong value of unsigned int variable in C -
i have written small program using c:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { unsigned int un_i = 1112; printf ("%d , %d", (1 - un_i), (1 - un_i)/10); return 0; }
my expectation is: "-1111 , -111"
but result is: "-1111 , 429496618"
i don't know why prints out 429496618 instead of -111. please explain me
i use gcc ver 4.4.7 , os centos kernel 2.6.32
thank much!
that because un_i
of type unsigned int
, not represent negative values. if expect result negative, need signed type, int
, try this:
unsigned int un_i = 1112; printf ("printf: un_i[%d] , u_i/10[%d]\n", (1 - un_i), (1 - (int)un_i)/10);
Comments
Post a Comment