Sunday 28 September 2014

Variable in C language

What is variable ?


Variable is the name or user define name that can be used to store values. Variable can take different type of values but it stores only one value at a time. These values can be changed during the execution of program. A data type is associated with each variable. The data type of the variable decides what values it can take or store.

Rule for naming variable-
1     
       The name should be consist of only alphabets (both upper and lower          case),digits and underscore sign.
     
        First character should be alphabets and underscore.
     
        The name should not be keyword.
   
        Since c is the case sensitive, the upper case and lower case are considered         different for e.g. Apple, APPLE both different.

Declaration of variable


We know that what is variable, it must to declare a variable before it is used in the program. Declaration of a variable specifies its name and data type. The type and range of values that a variable can store depends upon its data type.
Initialization of Variable-
When a variable is declare it contain undefined value known as garbage value.If we want we can assign some initial value to the variable during the declaration of variable. this is called initialization of the variable. for example: 
float x=8.9, y=98.0

Syntax for declaration of variable-

Data type <variable name ;>
Or
Data type <variable name1, variable name2 … n ;> 

For example-
                  
                   Int x;                       // declare variable named ‘x’

                   Float salary               // declare variable ‘salary’
                  
                   Char grade               // ‘grade’ is the variable

Here x is a variable of type int. this mean that x will take  the value of integer such as 12, 3, 4,etc. 
salary is a variable of type float, it take a value type of float like 3421.543 etc. and grade is a variable of type char. char means character it takes the value if character such as A, a, B, b, C,c etc.

declaration of mare than one variable-

int x,y,z,total; 

Here x, y,z and total  are all variable and type of variable is int / integer because all variable declare with int data type after declare variable we must terminate the variable with semicolon. in the upper example x, y,z,total are 4 variable x y z and total separate by the comma and variable declaration end by (;)semicolon. if we are not terminate the variable with semicolon our program generate a error. 



int a=5;
here a=5 means that 5 is assign to the variable a, if we write 'a' in all the program, 'a' means we write 5. 'a' and 5 both are equal after assigning the value. other initialization....

here x value is 8.9 and y value is 98.0 if we write x then its equal to 8.9 and if we write y then it is equal to 98.0.
char ch='y'
double num =0.4324;
int l,m,u,totle=0;
in the declaration only one variable is initialize.



No comments:

Post a Comment