954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

please tell me wht's GOTO

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?

nadith_cs
Newbie Poster
13 posts since Feb 2007
Reputation Points: 11
Solved Threads: 0
 

>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
Administrator
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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You