Here is my C code which I compiled and executed.
#include <stdio.h>
int main()
{
unsigned i = 0, j = 0;
unsigned s = 0;
for (; i <= 1; i++)
for (; j <= 1; j++)
s++;
printf("%u\n", s);
return 0;
}
I expected to see 4, but I saw 2. I can not understand why?
It's in your title: you don't initialize j in the inner for.
So the sequence will be: (i,j):
0,0
0,1
0,2 (inner for won't enter body)
1,2 (inner for won't enter body)
2,2 (outer for won't enter body)
In situations like this, you should write on paper step by step each variable value and check every test. The next step is to learn how to use the debugger. It's immensely helpful and will spare you a lot of headaches. I can't emphasize enough how much easier your life will be made by a debugger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With