Variables In C

Variables in C are memory locations that are given names that are used to refer a value and this value varies or change during the program executions. We use variables to store data in memory for latter use. It is also known as container of data.


Char: The character type (char) occupies one bytes of storage and can express a whole number in the range of -128 to 127.Unsigned characters Have a range of 0 to 255.
Typical declarations of characters types are shown below:

Char ch;     /*Declare a character variable */

Char alpha = ‘a’;     /*Declare character variable alpha and initialize to it */


Int: It refers to integer. It can hold a signed or unsigned number within the Specified range

Flot: It refers to floating point or real number. It can hold a real number like 3.174813 or 4.52e6 with six decimal digits in decimal or exponential form a flot number using 6 decimal digits is called a single precision number.

Double: It also refers to floating point or real number. It can hold a real number like in double precision. A double precision number uses 12 decimal digits like 3.412587632156 or 4.52145342148e12.

Declaration of Variables

Declaration of variable does two things.

  • • It tells the compiler what the variable name is.
  • • It specifies what type of data the variable will hold.

The declaration of variable must be done before they are used in program for computation. The general format of variable declaration is

Datatype name 1, name2, …. namen;

Datatype is the type of data used and name1, name2, …namen are the names of variables.


The scope of a variable determines the area of the program where that variable is valid i.e. the the parts of the program that have access to that variable. It depends on where it is declared. The life span of the variable determines the period during which a variable retains the value during executions of a program. The variables may also be broadly categorized, depending on the place of their declaration as I) Global variable, ii) Local variable.

Global variables
  • • They are declared outside the function.
  • • Its scope is through out the program.
  • • Its life span is also through out the entire program.
  • • They are automatically initialized to 0.
Local variables
  • • The declaration is placed after the opening curly brace { of any function including main (), and before any function statements.
  • • Scope of a local variable is limited to the function in which it is declared.
  • • Life span of a local variable is within the block in which it is declared.

/*Example of local and global variables*/

Int I;    /*i is global variable and its scope is valid through out the entire program*/

Void main ()
{
    Int n,         /*n is local only main () */
    Float sum;     /* sum is local only to main () */
    . . . .
    . . . .
    Function ()
}

Void function ()
{
    Int j;             /* j is local to function 1() and its scope is valid within function 1() */
    Float average;     /* average is local to function 1() */
    . . . .
    . . . .
}

In the above example, the variable I is declared before main () which is called as global variable, it can be used in all the function in the program. It need not be declared in other functions. The variable n, sum, j, average are called local variables. They are visible and meaningful only inside the function in which they are declared. They are not known to other functions. If a name is declared in more than one function the value of that name is restricted to that function only. Any change to the value of that variable in one function does not affect its value in the other.

C supports a variety of storage class specifier that can be used declare explicitly the scope and life time of variables. There are four storage class specifiers whose meanings are given in the below table.


img

Storage Classes and Their Meaning


The storage class also can be added with the qualifier like long or unsigned in the declaration.

Auto int num;
Static into I;
Extern long sum;
Register int p;

Static and external (extern) variables are automatically initialized to zero. Automatic (auto)variables must be initialization explicitly, otherwise it contains undefined or garbage value.


Assignment and initialization of variable

The value of a variable can be assigned some value by using assignment operator (=).

Variable _name = constant;

For example

Int a, I; /* value 100 is assigned to the variable a */
A = 100; /* the value of the expression i+1 is assigned to I */
I = i+1;

It is also possible to assign a value to a variable at the time of declaration. The general from is

Datatype variable _name = constant;



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext