3

If I write:

int a = 1;
int a = 2;

I get a compile time error, however if I write:

while(1) {
    int a = 1;
} 

no compile time error - whats the difference? does the while loop have its own scope or something?

2
  • 1
    @Benoit: Local variables can declared at the beginning of a block in C89, and anywhere in C99. So second code above is valid C89 and C99. Commented Mar 1, 2012 at 15:26
  • i'm glad I noticed this - the behaviour was not as I initially thought, would have been a source of bugs for sure. thanks for prompt and concise explanations Commented Mar 1, 2012 at 15:36

8 Answers 8

4

In the second case, you don't define multiple variables with the same name in the same scope. What happens is this

while(1)  { //there is no variable a in this scope
   int a = 1; //define a variable a at this point.
} //variable a is no more

In the first case, however, we have

int a = 1; //define variable a;

int a = 1; //variable a has been defined in this scope already, so error!
Sign up to request clarification or add additional context in comments.

Comments

4

Yes, each iteration of the loop has its own scope. When an iteration reaches loop end, variables defined inside are gone.

Comments

3

Yes, the braces of the loop open a new scope, all the variables in the inner scope hide variables with the same names from the outer scope.

Comments

2

You guessed it!

C allows you to redefine an identifier in a sub-scope. So the while block, being a different scope, allows you to shadow the variable a - of course, only valid inside that sub-block. That can cause a lot of problems if you're not aware of this behaviour.

Comments

1

Yes the {} define a new scope.

Comments

1

Yes, the loop block is a scope on its own. When the end of the loop is reached, a goes out of scope and a new a can be declared for the next iteration.

Comments

1

The difference is in the scope. The scope is determined by the curly braces - {}. In the first case both variables share the same scope - that's why you have received an error. In the second case you define local (for the scope of while) variable. A new variable is created on each iteration of the loop. This is perfectly valid and that's why you don't get error. If this is unclear to you I'd suggest researching it more, here is a good start - http://crasseux.com/books/ctutorial/Scope.html

Comments

1

After you do int a = 1;, you leave a { } block, removing every variables declared in that block. As you are doing a while (true) loop, you will enter another { } block but that new block will not be 'aware' that you previously created an 'a' variable... because it was swept away !

For example:

{
   int a = 1;
}
a = 2;

will not compile for the same reason

Edit: that's crazy, within a minute there was already five answers !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.