edits: 01/06/2008


Puzzles E11 ... E20

Block Scope and File Scope

These puzzles involve code where some variables have block scope and others have file scope.

An identifier has block scope if it is declared inside a block. Also, the parameters of a function have block scope for the block that is the body of the function.

An identifier has file scope if it is declared outside of any block or parameter list.

An identifier with file scope is visible from where it is declared in its file up to the end of the file, except within blocks that use the same identifier in a parameter list or a declaration.

The names of functions have file scope, so a function name is visible from where it is defined to the end of the file.


Puzzle E11

What does the following code write to the monitor?

#include <stdio.h>

int funct( int a, int b )
{
  int c=3 ;
  printf("scope f: a=%d    b=%d    c=%d\n", a, b, c );
}

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

  system("pause") ;
  return 0 ;
}

The two parameters a and b are defined in the parameter list of the function, and so have block scope for the main block of that function. The parameters are assigned values 1 and 2 when main() calls the function.


Puzzle E12

What does the following code write to the monitor?

#include <stdio.h>

int g = 777;

int funct( int a, int b )
{
  int c=3 ;
  printf("scope f: a=%d    b=%d    c=%d    g=%d\n", a, b, c, g );
}

int main ( void )
{
  int a=77, b=99 ;
  
  funct( 1, 2 ) ;
  printf("scope m: a=%d    b=%d    g=%d\n", a, b, g ) ;

  system("pause") ;
  return 0 ;
}

The variable g has file scope. It may be used anywhere in the program unless hidden by another identifier g defined in a block.


Puzzle E13

What does the following code write to the monitor?

#include <stdio.h>
int g = 777;

int glue( double g )
{
  printf("scope g: g=%lf\n", g );
}

int funct( int a, int b )
{
  int g=3 ;
  printf("scope f: a=%d    b=%d    g=%d  \n", a, b, g );
}

int main ( void )
{
  int a=77, b=99 ;
  
  funct( 1, 2 ) ;
  glue ( 3.14 );
  printf("scope m: a=%d   b=%d   g=%d \n", a, b, g ) ;

  system("pause") ;
  return 0 ;
}


Puzzle E14

Will the following code compile? If so, what does it write to the monitor?

#include <stdio.h>

int g = 777;

int g( int a, int b )
{
  int g=3 ;
  printf("scope f: a=%d   b=%d   g=%d\n", a, b, g );
}

int main ( void )
{
  g( 1, 2 ) ;
  system("pause") ;
  return 0 ;
}


Puzzle E15

Will the following code compile? If so, what does it write to the monitor?

#include <stdio.h>
int g( int a, int b )
{
  int g=3 ;
  printf("scope g: a=%d    b=%d   g=%d\n", a, b, g );
}

int main ( void )
{
  g( 1, 2 ) ;
  system("pause") ;
  return 0 ;
}

This program is a slight variation of the previous one. Pay attention to the identifier g.


Puzzle E16

Will the following program compile? If so, what does it write to the monitor?

#include <stdio.h>
char a=8, b=9;

int g ( int x, int y);

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

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

  system("pause") ;
  return 0 ;
}

The third non-blank line of the program is a function prototype, a declaration (but not a definition) of a function. In this program it is not needed, since the function definition follows immediately.

It does not hurt to have an unneeded prototype. Sometimes programmers put a prototype for every function in a file at the beginning of that file.


Puzzle E17

Question 1: Will the following program compile and run?

Question 2: Will it produce the correct result?

Question 3: Fix or improve the program.

#include <stdio.h>

int dum( int x )
{
  x -= 1 ;
  x = dee(x);
  return x;
}

int dee( int a )
{
  if ( a > 10 )
    a = dum(a);
  else
    a -= 2 ;

  return a;
}

int main ( void )
{
  int x;
  
  x = dum(7);
  printf("scope m: x=%d\n", x);

  system("pause") ;
  return 0 ;
}

Recall the following rule: If an identifer that has not been previously declared occurs in a program and is followed by a '(' , then the identifier is implicitly declared to be a function returning int. Nothing is assumed about the parameters of the function.


Puzzle E18

Question 1: Will the following program compile and run?

Question 2: Will it produce the correct result?

Question 3: Fix or improve the program.

#include <stdio.h>
double dum( double x )
{
  x -= 1.0 ;
  x = dee(x);
  return x;
}

double dee( double a )
{
  if ( a > 10.0 )
    a = dum(a);
  else
    a -= 2.0;

  return a;
}

int main ( void )
{
  double x;
  
  x = dum(7.0);
  printf("scope m: x=%lf\n", x);

  system("pause") ;
  return 0 ;
}

This is nearly the same as the previous program except that the variables are now type double. Will that make a difference?


Puzzle E19

Inspect the following program:

#include <stdio.h>

int j;

int fact( int n )
{
  int value = 1;
  for ( j=1; j<=n; j++ ) value *= j;
  return value;
}

int main ( void )
{
  for ( j=1; j<7; j++ )
    printf("factorial of %d is %d\n", j, fact(j) );

  system("pause") ;
  return 0 ;
}

Its output is the following:

factorial of 2 is 1
factorial of 4 is 6
factorial of 6 is 120

Explain this strange result.


Puzzle E20

What does the following code write to the monitor?

#include <stdio.h>

int x = 0 ;

int funA( int x )
{
  return x+1;
}

int funB( int x )
{
  return funA( x+1 );
}

int funC( int x )
{
  return funB( x+1 );
}

int main ( void )
{
  printf("funC is %d\n", funC( x ) );

  system("pause") ;
  return 0 ;
}

This puzzle is not particularly tricky. Just a lot of different x's to keep track of, and a rule about initialization.


— Return to the main contents page