i have a doubt in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

i have a doubt in c

why does this code outputs 1 int main(void) { int c=0; unsigned int a = 0; for(a=0; a<10; a--) c++; printf("%d", c); return 0; } i know that we have unsigned int here that cant go below 0 so it will go highest integer limit and end the loop there but when i do this : int main(void) { unsigned int a = 0; a--; printf("%d", a); return 0; } this prints -1 . well what is going on i think i am missing something HELP HERE......

19th Jul 2020, 3:00 PM
NIK
NIK - avatar
3 Answers
+ 2
ohh man ..........how can i forget that .......thank u bro, so much .
19th Jul 2020, 3:07 PM
NIK
NIK - avatar
+ 1
You need `printf("%u")` if you want to print unsigned integers, %d is for signed numbers. A signed -1 is `1111 1111` in binary which happens to be how the biggest unsigned number looks in binary aswell. So basically you are doing a "conversion" from unsigned to signed before printing.
19th Jul 2020, 3:06 PM
Schindlabua
Schindlabua - avatar
+ 1
The loop condirion in first snippet defines that loop goes as long as <a> is less than 10, and during loop <c> will be incremented. As you decrement <a> from 0 to 4294967295 the condition becomes false, and the loop only goes around once. That is how you got 1 in output ...
19th Jul 2020, 3:15 PM
Ipang