In C++, a variable is 'scoped' to the block in which it is declared. In other words, it exists from the point at which you tell the compiler what type of value it contains through to the close brace at that matches the open brace preceeding that declaration.
In the most frequent use, this means that a variable defined inside a loop doesn't exist after the loop has been exited. And that's very useful indeed in clearing down temporary memory storage for use within the loop's iterations. If you should happen to have another variable of the same name in the outer code (usually a daft thing to do!), its contents will NOT be visible within the inner block as they'll be masked by the more local variable, but they will be unmasked after the outer block is exited.
You can even set up a block that's
not a loop and
not a conditional - that's there purely to scope variables - if you wish. Simply use a pair of culry braces without any
if,
while or other keyword. Here's a code example that demsonstrates these features:
#include <iostream>
using namespace std;
int main () {
int demo[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int k;
int answer = 42;
for (k=0; k<12; k++) {
int answer = demo[k]; /* masks the original answer */
cout << answer << ", ";
}
cout << endl << "hidden was " << answer << endl;
{ /* This block is only for scoping purposes */
float answer = 3.1415;
cout << answer << " is something to do with cherry pie" ;
}
cout << endl << "hidden was " << answer << endl; /* the original again */
}
When I run that code, my results look like this:
[trainee@easterton lp]$ ./vscpp
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
hidden was 42
3.1415 is something to do with cherry pie
hidden was 42
[trainee@easterton lp]$ (written 2009-01-22, updated 2009-01-23)
Associated topics are indexed under
C231 - C and C based languages - Introduction to C++ [3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3053] Make - automating the commands for building and installing - (2010-11-16)
[3052] Getting your C++ program to run - (2010-11-15)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[2169] When should I use OO techniques? - (2009-05-11)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[336] Targetted Advertising - (2005-06-05)
[318] Choosing a theme - (2005-05-20)
[317] Programming languages - a comparison - (2005-05-20)
C234 - C and C based languages - Further C++ Object Oriented features [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3057] Lots of things to do with and within a C++ class - (2010-11-16)
[2849] What are C++ references? Why use them? - (2010-07-02)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2576] What does const mean? C and C++ - (2010-01-15)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[1819] Calling base class constructors - (2008-10-03)
[1159] It can take more that one plus one to get two. - (2007-04-22)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[802] undefined reference to typeinfo - C++ error message - (2006-07-15)
[801] Simple polymorphism example - C++ - (2006-07-14)
Some other Articles
The Month Ahead - What is happening in MelkshamLaunch of Melksham Food and Drink FestivalContrastVariable Scope in C++Discount Training Courses - PHP, Perl, PythonNew C Examples - pointers, realloc, structs and moreI have not programmed before, and need to learn2000th article - Remember the background and basicsHow low can you sink?