Constants And Variables; Local Variables And Global Variables - Casio FX-890P Owner's Manual

Casio personal computer owner's manual
Table of Contents

Advertisement

6.1 Constants and Variables

6.1.1 Local variables and global variables

Local variables
A local variable is one that is declared within a function for use within that particular
function only.
Since such variables are local, you can use the same variable name in multiple
functions without any problem – each variable is treated independently, even though
they have the same name. Have a look at the program listed below.
/* Local variable */
/* #include <stdio.h> */
void pr() /* print function */
{
int i; /* local i */
for(i=0;i<2;i++)
printf("%d ",i);
}
main(){
int i;
for (i=0; i<10; i++)
pr();
}
This program displays ten sets of numbers 0 and 1. Both the main() function and the
pr() function utilize variable "i" but the computer treats these as two entirely different
variables. In the main() function, the value of "i" ranges from 0 to 9, while in pr() it is 0
or 1 only.
Global variables
A global variable is one that is commonly applied throughout all functions of a
program. Global variables are declared in the line before preceding the main() line
and the other functions using them. Have a look at the program listed below.
/* Global variable */
/* #include <stdio.h> */
int i /* global i */
void pr() /* print function */
{
printf("%d ",i);
}
main(){
for (i=0; i<10; i++)
pr();
}
102

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Z-1grZ-1

Table of Contents