The type of NULL
and the type of i
are different, but C
forgives you :)
C
is (mostly) not a "type-safe" language. I mean: it lets you mix types which other languages would complain.
With C
you can add chars
to doubles
, you can multiply chars
and ints
, ...
What you are doing is assigning a null pointer constant
(of type void *
) to an int
. C
allows that, but you need to be very careful when mixing types like this.
The result of assigning the null pointer constant to an int is the same as assigning 0
to it. So you start your loop with i
being 0
.
A static auto variable, in the absence of an initializer, is initialized to 0
. This happens to the variable count
in your program.
So, the loop goes first (count
becomes 1) with i
being 0, then i
becomes 2.
Now the loop goes (count
becomes 2) with i
being 2, then i
becomes 4.
Now the loop goes (count
becomes 3) with i
being 4, then i
becomes 6.
And the loop terminates ... and you print the value of count
.
Notes
- to be Standard compliant
main
should be declared int main(void)
- you should output a newline after every complete line (
printf("%d\n", count);
)
- and you should
return 0;
to indicate successful completion of your program to the Operating System