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.
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.
Correct the above example by adding an argument to the function fct()
so that the program compiles without error. Test the program:
n: void fct (int n);i: void fct (int i);
#include <stdio.h>
// The value of i is passed as an argument
// The argument can be named n or i: in both cases,
// it is a local variable of fct(), distinct from i in main()
void fct(int n);
int main(void) {
int i=5;
fct (i);
return 0;
}
void fct(int n) {
printf ("%d\n", n);
}
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;
}
#include <stdio.h>
// Function that does n=n+1
void increment(int n);
int main(void) {
int n=3;
printf ("Before, n=%d\n", n);
increment(n);
printf ("After, n=%d\n", n);
return 0;
}
// Function that does n=n+1
// n is a copy of the variable of main(): the variable of main() is not modified
void increment(int n) {
n=n+1;
}
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;
}
#include <stdio.h>
// Function that returns n+1
int increment(int n);
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;
}
// Function that returns n+1
int increment(int n) {
return n+1;
}
In programming, variable scope...
Let's consider the following function:
void fct(int i);
Variable scope...
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
n. The variable in the main is not assigned.
Try again...