In the programs, generally we need to store values in the memory and perform operations on those values. This can be achieved in C, by the concept of variables. A variable is a placeholder for holding a value in the main memory (RAM). As the name implies, the value in the variable can change at any point of execution of the program. For using variables in our programs, there are essentially two steps:
1) Declare the variable
2) Initialize the variable
Declaring a Variable
Before using a variable in the program, we have to declare the variable. The syntax for declaring a variable in a program is as shown below:
The “type” in the above syntax represents the data type. The “variable-name” is the identifier. There are certain rules that must be followed while writing the variable name. They are as follows:
- A variable name must always start with an alphabet (letter) or an underscore ( _ ).
- The variable name must not be more than 31 characters. The suggested length of a variable name is 8 characters.
- C is case sensitive. So, the variable name “average” is different from “AVERAGE”.
- Keywords must not be used for declaring variables.
- White spaces are not allowed within the variable name.
Initializing a Variable
After declaring the variable, we can assign a value to the variable. This process of assigning a value to the variable is known as initialization. Syntax for initializing a variable is as shown below:
The value we assign to the variable depends on the data type of the variable.
The declaration and initialization can be combined into a single line as shown below: