Fundamentals of C

Type Declaration

All the variables must be declared before they can be used. Declaration does two things:

  • It tells the compiler name of the variable.
  • and the type of data that the variable will hold.

Variable declaration has the following form:
<Type-specifier>  <comma-separated-list-of-variables>;

e.g.
int a,b,c;  //defines integer variables a, b and c
char ch;  // defines character variable ch

As we declare the variable in the above manner, the compiler creates a space for the same in the memory and attaches the given name to that space. The variable can now be used in the program.

A value is stored in a variable using assignment operation. Assignment is of the form:
<Variable-name> = <value>;

It must be taken into consideration that the variable must be declared before assigning a value.

C also allows assignment of a value to a variable at the time of declaration. It takes the following form:

<Type-specifier>  <variable_name> = <value>;

e.g. int a = 25;
The declaration creates an interior type variable a and at the same time stores the value of 25.
The process of assigning initial values to the variables is known as initialization. More than one variable can be initialized in one statement using multiple assignment operators.
e.g.

  • int a = 3, b = 10;
  • int j,m; j = m = 2; // j=m=2 can also be defined on another line.. as per user readability.

There is an exception worth noting. In the following example:
int a , b = 20, c;

Here, the assignment will be made made in the following manner:
a=0
b=20
c=2323 (this is a garbage/random value, uninitialized)

Leave a Reply

Your email address will not be published. Required fields are marked *