C VARIABLES –
Talking about C VARIABLES, a variable is a name which we can use to store values. Generally, we can say that,
“A Variable is a named location which one may use to hold a value that the program may modify further.”
Furthermore, Variables may take different values but only one at a particular time. Also, we change the particular values during execution of the program. We should also remember that before using the variables, we must declare them in the program.
Certainly, there is a data_type with each variable which the user declares in the program. Exactly, the data_type of the variable decides what values it can or it will take. Also, the rules for naming C VARIABLES are same as that for naming C IDENTIFIERS.
GENERAL SYNTAX FOR DECLARING A VARIABLE –
Above is the general Syntax of declaration of a variable. Likewise identifier, the data_type is usually chosen by the user that may be int, char, float, long, double, etc. Furthermore, the Variable_name may consist of one or more valid Identifier names seperated by comma.
DECLARATION OF VARIABLES :
Generally, declaration of a variable specifies its name and data_type. Also, the type and range of values that a variable can store depends upon its data_type.
Some examples of declaration of variables are –
- int num;
Here, num is a variable of integer data_type..
- float percentage;
Similarly, here percentage is a variable of data_type float..
- char grade;
Finally, here grade is variable of character data_type..
Also, there’s one another way for multiple declaration of Variables..
- int a, b, c;
Here, a, b, c are three variables and all of them are of integer data_type.
- float avg, marks;
Similarly, here avg and marks both variables are of float data_type.
INITIALISATION OF VARIABLES :
When a user declares a Variable, it contains an undefined value which is commonly known as GARBAGE VALUE. Generally, a user can assign some initial value to the variable during the declaration itself, this is Initialisation of variable.
Some examples of Initialisation of variables are,
- int num=10;
Here, num is a variable of int data_type. The value of num is an integer value i.e. equal to 10..
- float avg=96.07;
Similarly, here avg is a variable of float data_type. The value of avg is floating value i.e. equal to 96.07..
- char grade=’A’;
Finally, here grade is a variable of char data_type. The value of grade is a character value i.e. equal to ‘A’..
Likewise declaration, there’s one another way also for multiple Initialisation of Variables..
- int a=1, b=2, c=3;
Here, a, b, c are three variables of integer data_type having values 1, 2, 3..
- float num=12.8, avg=46.55;
Similarly, here num and avg are two variables of float data_type having values 12.8 and 46.55..
Thus, it was all about C VARIABLES..And also, There comes an end to C IDENTIFIERS AND VARIABLE – A BRIEF DESCRIPTION..
FOR FURTHERMORE INFORMATION REGARDING PERVIOUS POSTS ABOUT C ELEMENTS..Click on,