>this works as infinite unconditional loop is'nt it?
Yes.
>y won't u get the error the redefinition of var x?
Why would you? The variable is only defined once at any given time. It's exactly the same as if you did this:
while ( 1 ) {
int x = 9;
}
Well, notexactly the same, but the example still stands. ;) Now, in practical terms, the variable isn't likely to actually be inside the loop such that every iteration causes the storage to be reallocated. It's just only visible within the loop body and reinitialized with every iteration.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
so can you explain how this variable declaration works when it get repeated.
y won't u get the error the redefinition of var x?
Think of this as declaring a variable in the naked block, whose scope and lifetime is limited to the block in which it is declared.
int g_myVariable = 100 ; // global scope and lifetime
int main (void)
{
int main_myVariable = 100 ; // local to the function main
for ( int i = 0; i < 10; ++i ) // scope of i is the entire loop block
{
int j = 0 ; // j is destroyed after each iteration and a new
// one is created at the start of every iteration
}
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734