Definition of Variable:
Variables are named storage locations in memory. The primary purpose of variable is to store data for later use. Variable is like a container (storage area) with a name in which we can store data. To indicate the storage area, each variable should be given a unique name. Variable names are just the symbolic representation of a memory location. By using a variable name we are referring to the data stored in the location.
The value of a variable may change during program execution, and we can also reuse it multiple times. Every variable in C language has some specific type that determines the size of the memory of the variable, the range of values that the memory can hold and the set of operations that one can perform on that variable.
Rules for Naming a variable in C:
There are some rules on choosing variable names. The following rules must be followed while naming variables:
- A variable name consists of letters (both uppercase and lowercase letters), digits, and the underscore.
- The first character must be a letter or an underscore. It can’t start with a digit.
- Variable names are case sensitive. The compiler treats the upper and lower case letters as different.
- No whitespace is allowed within the variable name.
- Special symbols such as period, semicolon, comma, slash etc. are not allowed other than underscore.
- Any reserved word (keyword) cannot be used as a variable name. e.g. int, float etc.
Declaring and Initializing Variables:
Variable Declaration:
In C programming, variables must be declared before they are used. The declaration of a variable informs the compiler about two things:
- The name of the variable
- The type of data the variable will stored
Variable names should be user friendly. The name should be chosen to describe the role that the variable is designed to play e.g., Emp_no for employee number. A meaningful name given to a variable always helps in better understanding of the program.
Variable declaration begins with the data type, followed by the name of one or more variables. All declaration statement must end with a semicolon. The general syntax for declaration of a variable is given below:
Syntax:
data_type variable_name;
To declare more than one variable of the specified type, use a comma separated list.
Syntax:
data_type variable1, variable2, …,variablen;
Example:
int roll_no;
char choice;
float fees, avg;
Variable Initialization:
Variable initialization means assigning values to the variables. C variables declared can be initialized with the help of assignment operator.
Different ways of initializing a variable in C:
Method 1: Declaring the variable and then initializing it
int roll_no;
roll_no=1;
Method 2: Declaring and initializing the variable together
int roll_no=1;
Method 3: Declaring multiple variables simultaneously and then initializing them simultaneously.
int a,b,c;
a=b=c=10;
Programming Example:
Program to input marks of 5 subjects of a student and calculate total, average and percentage of all subjects. (Assume that maximum marks obtained by a student in each subject is 100).
Output:
Declaring a variable as constant:
Variables can be declared as constant by using the const keyword or the #define preprocessor directive. Details about these are given as follows:
The const keyword:
Variables can be declared as constants by using the “const” keyword before the data type of the variable. The constant variables can be initialized only once. The default value of constant variables is zero.
The correct way to declare a constant in C is:
const datatype variable_name = value
For example:
const int var = 15
Adding the const keyword in the definition of the variable ensures that its value remains unchanged in the program.
A program that demonstrates the declaration of constant variables in C using const keyword is given as follows:
Example 1:
#include<stdio.h>
int main()
{
const int a;
const int b=5;
printf(“ The default value of variable a : %d”, a);
printf(“ The default value of variable b : %d”, b);
return 0;
}
Output:
The default value of variable b : 5
The #define preprocessor directive:
#define variable_name value
· variable_name: It is the name given to constant.
· value: This refers to any value assigned to variable.
A program that demonstrates the declaration of constant variables in C using #define preprocessor directive is given as follows:
Example:
#include<stdio.h>
#define num 10
int main()
{
printf(“The value of num is: %d”, num);
return 0;
}
Output: The value of num is: 10
Declaring a variable as volatile:
C’s volatile keyword is a type qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time without any action being taken by the code the compiler finds nearby. The implications of this can be quite serious, and sometimes software experts testify in regard to product failures.
Proper Use of C’s volatile Keyword:
A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:
- Memory-mapped peripheral registers
- Global variables modified by an interrupt service routine
- Global variables accessed by multiple tasks within a multi-threaded application