C Programming

variable declaration

The variables are used to store the data values. Before storing a value into the variable, the variable must be declared. The format for the variable declaration is as follows.

     data_type v1,v2,...vn;

Here, data_type is one of the valid data type of the C. It is followed by the list of variables separated by commas. The v1,v2, ..., vn are the name of variables. The ;(semicolon) shows the end of the declaration statement. The following are examples of valid declarations.

     int max;

     int sum, average;

     float temp;

     double pie;

The common rule applied to each variable is that the variable is declared before use. The variables are normally declared in beginning of the function body. The following program shows the declaration section in function body.

#include <stdion>
void man(){
     /*...declaration section...*/
     int max;
     float average;
     char ch;
     */...statement section begins from here...*/
     ...
     ...
     ...
}

Assigning values variables

After declaring variables, they are assigned values i.e initialized using assignment operator = as follows.

          variable name = values;

The below following statement are examples of assigning values to the variables.

          length = 5;
          breadth = 10;

The variable can also assign the value to the variable of the expression as follows.

          area = length * breadth;

It is also possible to assign the value to the variable (initialize) at the time of declaration with the following format.

          data_type variable_name = value;

The examples are.

          int length = 5;
          int breadth = 10;

which can also be written as

          int length = 5, breadth = 10;

The const word can be used make the variable constant as follows.

          const int max = 5;

If attempt is made to change the value of variable max in program, the compiler will issue an error.

#include <stdion>
void main(){
     */...Declaration of variables...*/
     char c;
     int i;
     unsigned int ui;
     long int li;
     float f;
     double d;
     /*...Assigning values...*/
     c = 'A';
     i = 123;
     ui = 304U;
     li = 105062L;
     f = 123.483;
     d = 345.6789
     /*...Printing values of variables...*/
     printf("%c\n",c);
     printf("%d\n",i);
     printf("%u\n",ui);
     printf("%ld\n",li);
     printf("%f\n",f);
     printf("%f\n",d);
}

result

*********************************************

          A
          123
          304
          105062
          123.483002
          345.678900

*********************************************