3. Variable
4.1 Variable Description
Turbo C2.0 stipulates that all variables must be in use before they are used. A variable description statement consists of a data type and one or more variable names followed by. The variable description is in the following form:
Types < variables table >;
This type refers to the valid data type of the Turbo C2.0. A variable table is one or more identifier names, separated by "," between each identifier.
For example:
int I, j, K; unsigned char c, str[5], *p;
4.2 Variable kinds
Variables can be described in three places in a program: Inside a function, in the parameter definition of a function, or outside of all functions. Depending on the location defined, the variable can be divided into local variables, formal parameters and whole variables.
One, local variables
A local variable refers to a variable (sometimes called an automatic variable) that is described inside a function. With the keyword Auto to explain, when auto omitted, all the non-whole variables are considered to be local variables, so auto is actually never used.
Local variables are automatically generated when a function call, but not automatically initialized, with the end of the function call, the variable will automatically disappear, the next time this function is called automatically generated, but also to assign a value, exit and automatically disappear.
Second, formal parameters
A formal parameter is a variable defined in parentheses following the function name to accept arguments from the calling function. Formal parameters can function as other local variables within a function.
For example:
Puthz (int x, int y, int color, char *p)
{
int I, j, K; /* Define Local Variables * *
< program body >
}
where x, y, color, and *p are the formal parameters of the function, they can be used directly in the function without further explanation.
Three, the whole variable
A whole variable is a variable that is described outside of all functions, which is "visible" throughout the program, can be used by any function, and retains its value throughout the running of the program. The whole variable can be described anywhere in the program as long as it satisfies the two conditions before and after using it, and is usually described before the main function of the program.
For example:
#include <stdio.h>
int test; /* Define the whole variable * *
void F1 (int x, float y); /* Child Function Description * *
void F2 (void); /* Child Function Description * *
Main ()
{
test=5; /* Assign value to whole variable * *
F1 (20, 5.5); /* Call child functions with formal arguments F1 () * *
The value of/*test becomes 115*/
F2 (); /* Call F2 (), the value of test becomes 1150*/
}
void F1 (int x, float y)
{
float Z; /*z defined as local variable * *
Z=x*y; /* Calculate * *
Test=test+z;
}
void F2 (void)
{
int count=10; /* Define local variables and initialize * *
Test=test*count;
}
Because the whole variable can be used by any function in the entire program, it can be passed as a means of passing parameters between functions, but the whole variable is too much and the memory overhead becomes larger.