this is my prob.
i used goto as follows

int main()
{
l1:
int x-9;
goto l1;
return 0;
}

this works as infinite unconditional loop is'nt it?
this compiles well.which means there isnt an error.

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?

>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, not exactly 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.

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
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.