Lesson 8.5. Variable scope

Definition

The scope of a variable is the part of the code during which this variable can be used. There are 3 types of variables:

The scope of a variable is also the area of the code where memory is allocated (reserved) for this variable:

To limit the program's footprint on memory usage, we always try to reduce the scope of variables to use as little memory space as possible.

Example

Let's analyze the following example:

void fct(void);

int main(void) {
  int i=5;
  fct ();
  return 0;
}

void fct(void) {
  printf ("%d\n", i);
}

The variable i is defined in the function main(), its scope is limited to the main program. We say that i is a local variable. It cannot be used outside of main(). Therefore, it cannot be used in the fct() function. At compile time, the following error appears:

main.c:14:17: error: use of undeclared identifier 'i'
  printf ("%d", i);
                ^
1 error generated.
compiler exit status 1

This error indicates that the variable i is not declared in the function, since it is local to the main program.

Exercises

Exercise 1

Correct the above example by adding an argument to the function fct() so that the program compiles without error. Test the program:

Exercise 2

Write a function void increment(int n); which receives as parameter an integer n and which executes the following line: n=n+1;. Test your function with the following main program:

int main(void) {
  int n=3;
  printf ("Before, n=%d\n", n);
  increment(n);
  printf ("After, n=%d\n", n);
  return 0;
}

Exercise 3

Write a function int increment(int n); which receives an integer n as a parameter and returns n+1. Test your function with the following main program:

int main(void) {
  int n=3;
  printf ("before, n=%d\n", n);
  increment(n);
  printf ("Hum... n=%d\n", n);
  n = increment(n);
  printf ("After, n=%d\n", n);
  return 0;
}

Quiz

In programming, variable scope...

Check Bravo! The scope is the space where a variable can be used and the memory is of course allocated for this variable. Try again...

Let's consider the following function:

void fct(int i);
Check Bravo! An argument is also a local variable. Try again...

Variable scope...

Check Bravo! The memory space increases with the scope of the variables. Try again...

What the following program displays:

#include <stdio.h>
void fct(n);

int main(void) {
  int n=7;
  fct(n);
  printf ("n = %d",n);
  return 0;  
}

void fct(n) {
  n=n-1;
}
Check Bravo! There are two distinct variables named n Try again...

What the following program displays:

#include <stdio.h>
void fct(void);

int main(void) {
  int n=7;
  fct();
  printf ("n = %d",n);
  return 0;
}

void fct(void) {
  n=n-1;
}
Check Bravo! The variable n is local to the main() function and unknow in the function fct(). Try again...

What the following program displays:

#include <stdio.h>
int fct(int n);

int main(void) {
  int n=7;
  fct(n);
  printf ("n = %d",n);
  return 0;
}

int fct(int n) {
  return n-1;
}
Check Bravo! The value is returned by the function fct(), but unused in the main(). Try again...

What the following program displays:

#include <stdio.h>
int fct(int n);

int main(void) {
  int n=7;
  n=fct(n);
  printf ("n = %d",n);
  return 0;
}

int fct(int n) {
  return n-1;
}
Check Bravo! The value is returned by the function fct() and used in the main() function. Try again...

What the following program displays:

#include <stdio.h>
int fct(int n);

int main(void) {
  int x=7;
  x=fct(x);
  printf ("x = %d",x);
  return 0;
}

int fct(int n) {
  return n-1;
}
Check Bravo! The value of n is assigned to x by the return keyword Try again...

What the following program displays:

#include <stdio.h>
int fct(int n);

int main(void) {
  int n;
  fct(n);
  printf ("n = %d",n);
  return 0;
}

int fct(int n) {
  return n-1;
}
Check Bravo! There are two distinct variables named n. The variable in the main is not assigned. Try again...

See also


Last update : 11/16/2023