Using Variables
In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function.
To use variables in your C programs, you must know the following rules when giving the name to variables in C:
- The name can contain letters, digits, and the underscore character (_).
- The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended.
- C is case sensitive therefore the variable name num is Different from Num.
- C keywords can't be used as variable names. A keyword is a word that is part of the C language.
The following list contains some examples of legal and illegal C variable names:
Variable Name |
Legal or Not |
Num |
Legal |
Ttpt2_t2p |
Legal |
Tt pt |
Illegal: Space is not allowed |
_1990_tax |
Legal but not advised |
Jack_phone# |
Illegal: Contains the illegal character # |
Case |
Illegal: Is a C keyword |
1book |
Illegal: First character is a digit |
The first new thing that stands out is the first line of the body of main():
int num = 10;
This line defines a variable named 'num' of type int and initializes it with the value 10. This might also have been written as:
int num; /* define uninitialized variable 'num' */
/* and after all variable definitions: */
num = 10; /* assigns value 10 to variable 'num' */
Variables may be defined at the start of a block (between the braces {and}), usually this is at the start of a function body, but it may also be at the start of another type of block.
Variables that are defined at the beginning of a block default to the 'auto' status. This means that they only exist during the execution of the block. When the function execution begins, the variables will be created but their contents will be undefined. When the function returns, the variables will be destroyed. The definition could also have been written as:
auto int num = 10;
Since the definition with or without the auto keyword is completely equivalent, the auto keyword is obviously rather redundant.
However, sometimes this is not what you want. Suppose you want a function to keep count of how many times it is called. If the variable would be destroyed every time the function returns, this would not be possible.
Therefore it is possible to give the variable what is called static duration, which means it will stay intact during the whole execution of the program. For example:
static int num = 10;
This initializes the variable num to 10 at the beginning of the program execution. From then on the value will remain untouched; the variable will not be re-initialized if the function is called multiple times.
Sometimes it is not sufficient that the variable will be accessible from one function only or it might not be convenient to pass the value via a parameter to all other functions that need it.
|