// How variables are scoped in C++
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int scp1 = 0;
// scp1 visible here
cout << "Outer:" << endl;
cout << "scp1 = " << scp1 << endl; // 0
{
// scp1 still visible here
scp1++; // 1
cout << "Inner:" << endl;
cout << "scp1 = " << scp1 << endl; // 1
int scp2 = 0;
// scp2 visible here
cout << "scp2 = " << scp2 << endl; // 0
{
// scp1 & scp2 still visible here
scp1++; // 2
cout << "Innermost:" << endl;
cout << "scp1 = " << scp1 << endl; // 2
scp2++; // 1
cout << "scp2 = " << scp2 << endl; // 1
int scp3 = 0;
// scp1, scp2 & scp3 visible here
cout << "scp3 = " << scp3 << endl; // 0
} // <-- scp3 destroyed here
// scp3 not available here
// scp1 & scp2 still visible here
cout << "Inner:" << endl;
cout << "scp1 = " << scp1 << endl; // 2
cout << "scp2 = " << scp2 << endl; // 1
// cout << "scp3 = " << scp3 << endl; // compile error
} // <-- scp2 destroyed here
// scp2 & scp3 not available here
// scp1 still visible here
cout << "Outer:" << endl;
cout << "scp1 = " << scp1 << endl; // 2
// cout << "scp2 = " << scp2 << endl; // compile error
// cout << "scp3 = " << scp3 << endl; // compile error
return 0;
} // <-- scp1 destroyed here
/*
g++ Scope.cpp -o Scope
./Scope
Outer:
scp1 = 0
Inner:
scp1 = 1
scp2 = 0
Innermost:
scp1 = 2
scp2 = 1
scp3 = 0
Inner:
scp1 = 2
scp2 = 1
Outer:
scp1 = 2
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// variable shadowing or name masking
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int scp = 0; // scope
// outer scp visible here
cout << "Outer:" << endl;
cout << "scp = " << scp << endl; // 0
{
// outer scp still visible here
scp++; // 1
cout << "Inner:" << endl;
cout << "scp = " << scp << endl; // 1
int scp = 10; // redefine in inner scope
// inner scp visible here, outer scp no longer visible here
cout << "scp = " << scp << endl; // 10
{
// inner scp still visible here
scp++; // 11
cout << "Innermost:" << endl;
cout << "scp = " << scp << endl; // 11
int scp = 20; // redefine in innermost scope
// only innermost scp visible here
cout << "scp = " << scp << endl; // 20
} // <-- innermost scp destroyed here
// innermost scp not available here
// inner scp still visible here
cout << "Inner:" << endl;
cout << "scp = " << scp << endl; // 11
} // <-- inner scp destroyed here
// inner and innermost scp not available here
// outer scp still visible here
cout << "Outer:" << endl;
cout << "scp = " << scp << endl; // 1
return 0;
} // <-- outer scp destroyed here
/*
g++ Shadow.cpp -o Shadow
./Shadow
Outer:
scp = 0
Inner:
scp = 1
scp = 10
Innermost:
scp = 11
scp = 20
Inner:
scp = 11
Outer:
scp = 1
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// On-the-fly variable definitions in C
#include <stdio.h> // for getchar(), printf()
int main()
{
//..
{ // Begin a new scope
int q = 0; // old C requires definitions here
//..
// Define at point of use:
for(int i = 0; i < 100; i++)
{
q++; // q comes from a larger scope
// Definition at the end of the scope:
int p = 12;
} // inner p ends here
int p = 1; // A different p
} // End scope containing q & outer p
printf("Type a character ('q' to quit):\n");
char c;
while(c = getchar())
{
getchar(); // for Enter
if (c == 'q') {break;} // out of while()
// else
printf("'%c' wasn't it\n", c);
char x = c;
if (x == 'a' || x == 'b')
{printf("You typed 'a' or 'b'\n");}
else
{printf("You typed '%c'\n", x);}
}
for (int i = 0; i < 4; i++) // outer i
{
printf("Type A, B, or C\n");
int i; // redefine i within a for() iteration
switch(i = getchar())
{
case 'A':
printf("Snap\n");
getchar(); // for Enter
break;
case 'B':
printf("Crackle\n");
getchar(); // for Enter
break;
case 'C':
printf("Pop\n");
getchar(); // for Enter
break;
default:
printf("Not A, B, or C!\n");
getchar(); // for Enter
break;
} // end of inner i, go to the next for() iteration
} // end of outer i, end of for()
return 0;
}
/*
gcc onthefly.c -o onthefly
./onthefly
Type a character ('q' to quit):
a
'a' wasn't it
You typed 'a' or 'b'
b
'b' wasn't it
You typed 'a' or 'b'
c
'c' wasn't it
You typed 'c'
q
Type A, B, or C
A
Snap
Type A, B, or C
B
Crackle
Type A, B, or C
C
Pop
Type A, B, or C
D
Not A, B, or C!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// On-the-fly variable definitions in C++
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
//..
{ // Begin a new scope
int q = 0; // old C requires definitions here
//..
// Define at point of use:
for(int i = 0; i < 100; i++)
{
q++; // q comes from a larger scope
// Definition at the end of the scope:
int p = 12;
} // inner p ends here
int p = 1; // A different p
} // End scope containing q & outer p
cout << "Type a character ('q' to quit):" << endl;
while(char c = cin.get())
{
cin.ignore(); // for Enter
if (c == 'q') {break;} // out of while()
// else
cout << "'" << c << "' wasn't it" << endl;
if(char x = c) // if c non-null, c != '\0'
{
if (x == 'a' || x == 'b')
{cout << "You typed 'a' or 'b'" << endl;}
else
{cout << "You typed '" << x << "'" << endl;}
}
}
for (int i = 0; i < 4; i++) // outer i
{
cout << "Type A, B, or C" << endl;
switch(int i = cin.get()) // redefine i within a for() iteration
{
case 'A':
cout << "Snap" << endl;
cin.ignore(); // for Enter
break;
case 'B':
cout << "Crackle" << endl;
cin.ignore(); // for Enter
break;
case 'C':
cout << "Pop" << endl;
cin.ignore(); // for Enter
break;
default:
cout << "Not A, B, or C!" << endl;
cin.ignore(); // for Enter
break;
} // end of inner i, go to the next for() iteration
} // end of outer i, end of for()
return 0;
}
/*
g++ OnTheFly.cpp -o OnTheFly
./OnTheFly
Type a character ('q' to quit):
a
'a' wasn't it
You typed 'a' or 'b'
b
'b' wasn't it
You typed 'a' or 'b'
c
'c' wasn't it
You typed 'c'
q
Type A, B, or C
A
Snap
Type A, B, or C
B
Crackle
Type A, B, or C
C
Pop
Type A, B, or C
D
Not A, B, or C!
*/