0 votes 0 votes main() { unsigned int i= 255; char *p= &i; int j= *p; printf("%d\n", j); unsigned int k= *p; printf("%d", k); } Both the outputs are -1. I have even tried with - int i = 255(3rd line). Still the output is -1. I don't understand how it is -1. Programming in C programming-in-c output programming pointers + – Psnjit asked Jan 12, 2019 edited Mar 11, 2019 by Naveen Kumar 3 Psnjit 1.1k views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply arvin commented Jan 12, 2019 reply Follow Share %d is used to print signed values and %u for unsigned values.. 0 votes 0 votes Psnjit commented Jan 12, 2019 i edited by Psnjit Jan 12, 2019 reply Follow Share thanks 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes I guess we saw that happening due to incompatible type casting. When we use char *p = &i : due to signed / unsigned type casting (underflow) unsigned 255 becomes signed -1 hence the values of j and k. Try to run below: int main(){ unsigned int i = 255; int *p = &i; int j = *p; unsigned int k = *p; printf ("i : %d \n", i); printf ("j : %d \n", j); printf ("k : %d \n", k); return 0; } Let me know your thoughts. Utkarsh Kumar Raut answered Jan 12, 2019 Utkarsh Kumar Raut comment Share Follow See all 0 reply Please log in or register to add a comment.