Okay, I tried the examples and here's what I found:
Magos-- your example isn't even valid.
Code:
int main()
{
for(int i = 0; i < 5; i++)
{
int MyVar = 5;
}
MyVar = 6;
for(int LoopVar = 0; LoopVar < 5; LoopVar++)
{
}
LoopVar = 6;
return 0;
}
You can't declare an 'int' in the middle of your loop. If your compiler allows it, it's a low-quality compiler.
------------
Prelude-- Your first and second examples are invalid:
Code:
#include <stdio.h>
int main ( void )
{
int a;
for ( a = 0; a < 5; a++ ) {
int b = 0;
b += a;
}
printf ( "%d\n", b );
return 0;
}
and
Code:
int main ( void )
{
int a;
{
int b = 0;
b += a;
}
printf ( "%d\n", b );
return 0;
This code won't even compile in a professional grade 'C' compiler. If C++ would normally allow variable declarations this way, then it's because your implementation of a 'C' compiler happens to be generously allowing you to abstract where variables can be declared. This is not standard for 'C'.
In otherwords, in C, the above examples are syntactically incorrect and break during symbol table build.
------
I am using the latest version of C/C++ CodeWarrior, by MetroWerks. This compiler is used to build more software on every platform and game system than any other. It has become the defacto standard for the professional developer-- no i wasn't paid to say this (grin).
I really wanted to be able to compile it and then post the dissassembly so we cold see where the vars were allocated and how they were being accessed-- but since it's syntactically incorrection, I couldn't get that far.
Oh well.