edits: 01/06/2008


Puzzles E01 ... E10

Block Scope

These puzzles involve looking at code examples and predicting what will be written on the monitor. To do this, you need to figure out the scope of various variables. The scope of an identifier is the area of the program in which the identifier may be used. All the variables in these puzzles have block scope.

An identifier has block scope when it appears inside a block or appears in the parameter list of a function definition. Syntactically, a block consists of statements enclosed by left and right braces, { and }. An identifer with block scope follows these rules:

  1. An identifier is visible inside its block from where it is declared to the end of the block.
  2. Blocks may be nested within blocks. An identifier declared in an outer block is visible in an inner block if the declaration preceeds the inner block.
  3. It is OK if the same identifier is declared in an outer block and is also declared in an inner block.
    • The scope of the identifier declared in the outer block includes the part of the inner block up to where the identifier is declared again.
    • The scope of the identifier declared in the inner block starts where it was declared and continues to the to end of its block.
    • Usually a programmer would not use the same identifier in two nested blocks like this.
  4. When there are several blocks in sequence (nested in an outer block), the identifiers declared in one block cannot be seen in another block.
  5. The parameters of a function have block scope for the body of the function (which is a block).

Puzzle E01 — Rule 1

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int a = 1;
  int b = 2;
  
  printf("a=%d\tb=%d\n", a, b );
  
  system("pause");
  return 0;
}

The two variables a and b are defined at the beginning of a code block. They have block scope.


Puzzle E02 — Rule 2 and Rule 3

What does the following code write to the monitor?

#include <stdio.h>

int main( void )
{
  int a = 1;
  int b = 2;
  
  {
    int b = 3;
    printf("a=%d\tb=%d\n", a, b );
  }
  
  printf("a=%d\tb=%d\n", a, b );
  system("pause");
  return 0;
}

This program has a nested scope inside the main scope. At the beginning of that scope is a declaration of a variable, b, which has block scope for that block.


Puzzle E03 — Rule 4

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int a = 1;
  int b = 2;
  
  printf("main   : a=%d\tb=%d\n", a, b );
  
  {
    int b = 3;
    printf("block1 : a=%d\tb=%d\n", a, b );
  }
  
  {
    printf("block2s: a=%d\tb=%d\n", a, b );

    int a = 4;
    printf("block2e: a=%d\tb=%d\n", a, b );
  }
  
  printf("main   : a=%d\tb=%d\n", a, b );
  
  system("pause");
  return 0;
}

Here, there are two sequential inner blocks, each with a declaration of a variable. All variables in this program have block scope, but different variables have scope within different blocks.


Puzzle E04 — Rule 4

Will the following program compile without errors?

#include <stdio.h>
int main( void )
{
  int a = 1;
  int b = 2;
  
  {
    int b = 3;
    int c = 4;
    printf("a=%d\tb=%d\tc=%d\n", a, b, c );
  }
  
  {
    int a = 4;
    printf("a=%d\tb=%d\tc=%d\n", a, b, c );
  }

  system("pause");
  return 0;
}

Hint: consider the identifier c.


Puzzle E05

What does the following code write to the monitor?

#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;
}

There are now three separate variables, each named b, declared in three nested blocks.


Puzzle E06

Will the following code compile?

#include <stdio.h>
int main( void )
{
  int a = 1;
  int b = 2;

  if ( a == b )
  {
    int c = 3;
  }
  else
  {
    c = 5;
  }
  
  system("pause");
  return 0;
}


Puzzle E07

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int a = 7;
  int b = 1;

  if ( a >= b )
  {
    int temp = a;
    a = b;
    b = temp;
  }
 
  printf("a=%d\tb=%d \n", a, b );
  system("pause");
  return 0;
}


Puzzle E08

Examine the following code:

#include <stdio.h>
int main( void )
{
  int i ;

  {
    int b = 99;
    printf("b=%d\n", b );
  }
  
  {
    int a;
    printf("a=%d\n", a );
  }

  system("pause") ;
  return 0 ;
}

Question 1: Is it predictable what this program will write to the monitor?

Question 2: What might the program write to the monitor, mostly as an accident?


Puzzle E09

What does the following code write to the monitor?

#include <stdio.h>

int main( void )
{
  int a=1 ;
  printf("scope m: a=%d\n", a );

  if ( a )
  {
    int b = 2;
    printf("scope 1: a=%d\tb=%d\n", a, b );
    
    if ( b )
    {
      int c = 3;
      printf("scope 2: a=%d\tb=%d\tc=%d\n", a, b, c );
      a = b = c = 99;
      printf("scope 2: a=%d\tb=%d\tc=%d\n", a, b, c );
    }
    printf("scope 1: a=%d\tb=%d\n", a, b );
  }
  printf("scope m: a=%d\n", a );

  system("pause") ;
  return 0 ;
}

This puzzle is not particularly tricky (like the last one). Just routine application of the rules will do.


Puzzle E10

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int a=1, b=2, c=3 ;
  printf("scope m: a=%d\tb=%d\tc=%d\n", a, b, c );

  if ( a )
  {
    int b = 66;
    printf("scope 1: a=%d\tb=%d\tc=%d\n", a, b, c );
    
    if ( b )
    {
      int c = 77;
      printf("scope 2: a=%d\tb=%d\tc=%d\n", a, b, c );
    }
    printf("scope 1: a=%d\tb=%d\tc=%d\n", a, b, c );
  }
  printf("scope m: a=%d\tb=%d\tc=%d\n", a, b, c );

  system("pause") ;
  return 0 ;
}

As with P09, this puzzle is not particularly tricky . Just routine application of the rules will do.


— Return to the main contents page