In this tutorial, you’ll learn about variables, rules for naming a variable, constants and different type of constants in C programming.
In programming, a variable’s a container (storage area) to hold data.
To indicate the storage area that each variable should be given a unique name (identifier). Variable names’ just the symbolic representation of a memory location. For example:
int Score = 95;
Here, Score is a variable of integer type. Here, the variable’s assigned an integer value 95.
The value of variables can be changed, hence the name variable.
char ch = ‘a’;
// some code
ch = ‘l’;
Rules for naming a variable
1. The variable name can have the letter (both uppercase and lowercase letters), digits and underscore only.
2. The first letter of the variable should be either a letter or an underscore.
3. There’s no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name’s longer than 31 characters.
Note: You’d always try to give meaningful names to variables. For example, the first name is a better variable name than final Name.
C is a strongly typed language. This means variable type cannot be changed once it’s declared. For example:
int num = 7; // integer variable
num = 7.5; // error
double num; // error
Here, type of num variable’s int. You cannot assign floating-point (decimal) value 7.5 to this variable. Also, you cannot redefine the type of variable to double. By the way, to store decimal values in C programming, you need to declare its type to either double or float.