Technically, neither of those should compile. Variables defined in the for statement are valid only for the for statement. From memory, the Microsoft compilers allowed this, but GCC and Clang don't.
The standard says:
6.5.3
3) If the for_init_statement is a declaration, the scope of the name(s) extends to the end of the statement. [ Example:
1 2 3 4 5 6 7
int i = 42;
int a[10];
for (int i = 0; i < 10; i++)
a[i] = i;
int j = i; // j = 42
hello genson! thanks for your reply. And yes, you are right, looping with b = 0 shouldn't do much. Yet what I was trying to ask was how come when I try to print out b on line 4 of my second piece of code, the compiler throws an error saying b is not in scope, while the first piece of code just compiles. Perhaps you could shed some light onto this?