Introduction to Variables

Variables
It is used to store some value in ’C’ language program.A variable is symbolic name given to memory location which can store any type of value. When we create a variable we must specify the variable name and its data type. The variable name must be relative so that it reflects its purpose, function and nature.


We can perform two operations on the variables
i).Variable Declaration                                   
ii).Variable Initialization

i).Variable Declaration:-If we want to store a value in a C language program then we must create a variable. The process of creating a variable is called as variable declaration. Variable declaration tells the C language compiler what is the name of variable, the data type of variable

When we declare a variable compiler randomly allocate memory to the variable and store a garbage value to it. Each variable has a memory address.





Num
512
ß Value


1000
ßMemory Address


Note:- If we do not declare a variable before using it in C Language program then compiler display following error message
Error: - Undefined Symbol ‘Num’ in the Function main


Syntax1: -DataType VariableName;
Ex: - int Num1;

We can also declare multiple variable in a single statement.All the variable name must be separated by comma(,).

Syntax2: -DataType VName1,VName2..VName N;
Ex: - int Num1, Num2, Result;



/*WAP which is used to declare a variable.*/
#include<stdio.h>
#include<conio.h>
void main ()
{
     int Num1;
     float Marks
     vhar C;
     clrscr ();
     printf("Variables Created");
     getch ();
}




/*WAP which is used to print the value of variable and address.*/
#include<stdio.h>
#include<conio.h>
void main ()
{
      int Num1=10;
     
      clrscr ();
      printf("\nVariables Value %i",Num);
      printf("\nVariables Address %0x",&Num);
      getch ();
}

We can declare the variable inside the main function or outside the main function. If we declare a variable inside the main function then it is called as Local variable. If we declare a variable outside the main function it is called as Global variable.

Local Variable
Global variable
#include<stdio.h>
#include<conio.h>

void main ()
{
      int Num1=10;     ß Local Variable
     
      . . . . . . .
     
      . . . . . . . 
}

#include<stdio.h>
#include<conio.h>

int Num1=10; ß Global Variable
void main ()
{
           
      . . . . . . .
     
      . . . . . . . 
}


ii). Variable Initialization: -The process of assigning a value into the variable at the time of variable declaration is called as variable initialization. By default all the ‘C’ Language variable contains garbage value. It is assigned by compiler automatically when we declare a variable.

Syntax:-DataType VariableName=value;

Ex:-        int Num1=100;                    
               float PI=3.141;                    
               char ch=’C’;


/*WAP which is used to initialize  variables.*/
#include<stdio.h>
#include<conio.h>
void main ()
{
     int Num1=10;
     float Marks=90.5;
     char C='S';
     clrscr ();
     printf("Variables Initialize");
     getch ();

}

Comments