If you want a variable to retain its value across a for loop, you should always declare it outside. Regardless whether it is a class or a POD.
That best expresses your intention to the compiler and to your fellow software maintainers.
You win practically nothing by moving it inside the for-loop.
If you are determined on letting everyone know that this var is only used in the for loop scope, then you can add another scope around it.
using namespace std;
int main( int argc, char *argv[] )
{
{
char buffer[5];
for( int i = 0; i < 5; ++i )
{
if(i == 0)
{
buffer[0] = 'y';
buffer[1] = '\0';
}
cout << buffer[0] << endl;
}
}
return 0;
}
Taking into account that some compilers may be able to optimize the initialization out of the loop for you, makes the code unclear. Also this optimization is not stated in the standard, so compilers are free to not do that.