Declarations
To declare a variable in a C program one writes the type followed by a list of variable names which are to be treated as being that type:
typename variablename1,..,..,variablenameN;
For example:
int i,j;
char ch;
double x,y,z,fred;
unsigned long int Name_of_Variable;
Failing to declare a variable is more risky than passing through customs and failing to declare your six tonnes of Swiss chocolate. A compiler is markedly more efficient than a customs officer: it will catch a missing declaration every time and will terminate a compiling session whilst complaining bitterly, often with a host of messages, one for each use of the undeclared variable.
<h2>Declarations and Initialization</h2>
When a variable is declared in C, the language allows a neat piece of syntax which means that variables can be declared and assigned a value in one go. This is no more efficient than doing it in two stages, but it is sometimes tidier. The following:
int i = 0;
char ch = 'a';
are equivalent to the more longwinded
int i;
char ch;
i = 0;
ch = 'a';
This is called initialization of the variables. C always allows the programmer to write declarations/initializers in this way, but it is not always desirable to do so. If there are just one or two declarations then this initialization method can make a program neat and tidy. If there are many, then it is better to initialize separately, as in the second case. A lot means when it starts to look as though there are too many. It makes no odds to the compiler, nor (ideally) to the final code whether the first or second method is used. It is only for tidiness that this is allowed.
0 comments:
Post a Comment