scope m: a=1 b=2 scope 1: a=1 b=3 scope 2: a=1 b=4 scope 1: a=1 b=3 scope m: a=1 b=2
#include <stdio.h> int main( void ) { int a = 1; int b = 2; printf("scope m: a=%d\tb=%d\n", a, b ); { int b = 3; printf("scope 1: a=%d\tb=%d\n", a, b ); { int b = 4; printf("scope 2: a=%d\tb=%d\n", a, b ); } printf("scope 1: a=%d\tb=%d\n", a, b ); } printf("scope m: a=%d\tb=%d\n", a, b ); system("pause"); return 0; }
Comments:
Since there are no new declarations
of a
, each use of the identifier
"a"
refers to the same variable.
"b"
, however, is a different story. Each block starts with a
declaration of a variable b
. There are three different variables
"b"
, each with its own scope.
Notice how, for instance, the "b"
used in the second
printf("scope m: a=%d\tb=%d\n", a, b );statement refers to the variable
b
declared at the beginning of the
main block.