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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -