Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Scope in C++

If I had the following code:

for(int myvar = 0; myvar < 10; myvar++);
if(1)
{
    int var2 = 16;
}

Then, afterwards I wrote the following:

myvar = 0;
var2 = 0;

Would that be legal? My VC++6 compiles it correctly, but I think this should be illegal. (It gives a compiler error in one of my other compilers.)

like image 492
Mateen Ulhaq Avatar asked Nov 02 '10 06:11

Mateen Ulhaq


People also ask

What is scope of variable in C with example?

A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration. This tutorial guides you on how to use C variable scope. Inside a function or a block. Out of all functions.

What is variable scope example?

Variables have a global or local "scope". For example, variables declared within either the setup() or draw() functions may be only used in these functions. Global variables, variables declared outside of setup() and draw(), may be used anywhere within the program.

What is variable and its scope?

A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.

How many variables scopes are there in C language?

C has four kinds of scopes: block scope. file scope. function scope.


3 Answers

VC6 is rather old, and not always ... rigid ... in its application of the standard :-) It actually leaked scope in certain circumstances like:

for (int i = 0; i < 10; i++) { }
// You can still use 'i' here.

This led to some funky macro magic to get around this problem. If you're using a ISO-conformant compiler, both those things you try to do are illegal.

From ISO C++11 3.3.3/1, dealing with the introduction of block scope with {...}:

A name declared in a block is local to that block; it has block scope. Its potential scope begins at its point of declaration and ends at the end of its block.

Section 6.5.3 covers the scope of variables "created" by a for statement:

If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for-statement.

like image 170
paxdiablo Avatar answered Oct 06 '22 00:10

paxdiablo


No, it would not be (§3.3.2 Local scope):

  1. A name declared in a block (6.3) is local to that block. Its potential scope begins at its point of declaration (3.3.1) and ends at the end of its declarative region.

I recommend you use compilers released in the last decade.

like image 27
Matthew Flaschen Avatar answered Oct 05 '22 23:10

Matthew Flaschen


VC took about a decade to implement proper scope for variables declared in loops and conditional statements. Generally you can't rely on VC6' judgment regarding C++.

like image 34
sbi Avatar answered Oct 05 '22 23:10

sbi