in Study Resources retagged by
212 views
2 votes
2 votes
#include<stdio.h>
int main() {
int i;
for(i =-1;i<sizeof(i);i++)
printf("%d",i);
}

The output of the code is didn't enter to the loop and prints nothing.

Can anyone explain
in Study Resources retagged by
212 views

1 Answer

4 votes
4 votes

Its because here even though i = -1, but on comparison with sizeof(i) (which returns a unsigned value ‘4’), the compiler also treats i as unsigned number.

And as i = -1, then converting it as unsigned will result in Large number as the MSB bit would be set as 1.

If i would have equal to non negative number less than 4, it would have entered into the loop.

The above code shows the unsigned value of i, when it is equal to -1
The above output shows the unsigned value of i, when it is equal to -1

 

edited by

Related questions