back

Quick Test 15: Scope of variables, passing by value vs. passing by reference

forward
1. What is the scope of each object in the following program
float a;

void proc1(float b)
{
  float c;
  int d = 10;

  c = b+ (float) d;
  printf("%f", c);
}

float proc2(float *e)
{
  float f = 20.0;

  return(*e+f);
}

float g;

void main()
{
  float h;

  a = 10.0;
  proc1(a); 
  proc2(&a);
}

 
local
global
parameter
neither
a
b
c
d
e
f
g
h
2. Consider the program below
int x;

void show(int *a)
{
  printf("%d ", *a);
  *a = *a + 1;
}

void main()
{
  x = 0;
  printf("%d ", x);
  show(&x);
  printf("%d ", x);
}

The procedure is called using the technique of
Passing by value
Passing by reference
And, hence the output will be

3. What will be the output of the next program?

include<stdio.h>
/* double names */
int x;

void show()
{
  int x;

  x = 1;
  x = x*x;
  printf("%d ", x);
}

void main()
{
  x = 0;
  show;
  printf("%d ", x);
}

Cannot have two identical names for variables!
0 0
1 0
0 1
1 1