In this post of variables in C Programming, you will read about C Variables and learn how variables declaration and variables initialization are done in C programming.
Variable is the most important element in any C program, so read this tutorial thoroughly.
Variable in C – What is C Variable?
As we have read in the last tutorial that in all C programs, we need different types of data types like 25, ‘A’, 35.66 and all this data is stored in computer memory.
There are thousands of millions of cells (locations) in computer memory where you store different types of data.
Now because it is impossible for programmers to remember the numeric address of these thousands of millions of memory locations.
Therefore, before storing data on these memory locations, names (identifiers) are given to these locations which we call variables.
To understand in simple words, variables are the names of memory locations in programming, with the help of which data is kept or extracted in memory.
When you create variables in your programs, then identifiers (names) are being given to the memory locations in the backend.
Now because all this work is done backend (internal) so you do not have any meaning with memory locations or this backend process i.e. you just have to declare variables in your programs and use them.
Now whenever you hear the word variable, you will understand that variable means memory location, so you never say that my data is at this memory location, you only say that my data is stored in this variable.
You can also read: Prime Number Program in C Language
Variables in C Programming Naming Rules in C Language
Variable Declaration in C Language
Variable declaration is a process in which you are giving information about your variable to your C compiler like what is the name and data type of the variable.
Like all of us humans have a gender type associated with it, which shows whether the human is male or female, in the same way every variable has a data type associated with it.
From the data type of the variable, the compiler knows that how much space is to be allocated in the memory for the variable and what type of data will be stored in the memory.
When you declare variable then you first enter data type like int, char, float etc. After that the variable identifier ie name.
Apart from this, it must be remembered that it is very important to declare any C variable before using it in the program.
Let us now see the syntax and examples of variable declaration of primitive data types in c language.
Variables in C Programming
Variable in C Programming Variable Declaration Syntax 1:
Variables in C Programming Declaration Example 1:
int num; int num2; char ch; float height;
Variables in C Programming Variable Declaration Syntax 2:
data_type variable1, variable2, ... , variablen;
Variables in C Programming Variable Declaration Example 2:
int num, num2, age; char ch, gen; float avg, height, marks;
In the first syntax of variable declaration we have declared a variable and in the second syntax we have declared more than one variable of same data type with the help of comma ( , ).
Here you should keep one thing in mind that you cannot declare variables of different data types simultaneously with the help of comma. By doing this you may get an error show.
Do not forget to put a semicolon ( ; ) at the end of the variable declaration.
Variable Initialization in C Language
When you make a variable declaration like in the above examples, then the name and space in the memory for your variable gets assigned as well as a garbage value is also assigned to that variable.
You can call Garbage value as undefined value i.e. the value which is not you but the compiler has assigned your variable.
When you (programmer) assign value to a variable by yourself, then we call this process Variable Initialization.
The assignment operator ( = ) is used to initialize variables in C programming.
While initializing value to any variable, keep in mind that what is the data type of that variable because value is assigned to any variable according to its data type.
According to the use of the variable in your c programs, you can initialize the variable at the time of declaration or even after the declaration.
Variables in C Programming Initialization Syntax 1:
variable-name = value or expression;
Variables in C Programming Initialization Example 1:
int num, num2; float cgpa; num = 10; num2 = 12 + 5 - 3; cgpa = 8.4;
In the first syntax of variable initialization, we have declared variables first, after that assign values to those variables with the help of assignment operator.
Variables in C Programming Initialization Syntax 2:
data_type variable = value or expression;
Variables in C Programming Initialization Example 2:
int num = 10; char gender = 'M'; float cgpa = 9.7;
In the second syntax of variable initialization, we have initialized values to variables at the time of variable declaration.
Variables in C Programming Initialization Syntax 3:
variable1 = variable2 = variable3 = value or expression;
Variables in C Programming Initialization Example 3:
int num1, num2, num3; num1 = num2 = num3 = 15;
In the third syntax of variable initialization, we have initialized all the three variables (num, num2, num3) together to value (10). We follow this syntax when we have to assign the same value to more than one of our variables.
Variables in C Programming Initialization Practice Example:
int num1, num2, num3, sum; num1 = 10; num2 = 20; num3 = num1; num1 = 20; sum = num1 + num2 + num3;
To understand this example, read the explanation line by line and read it well.
On line 1 we have declared 4 variables of int data type then on line 2 and 3 we have assigned 10 values to num1 and 20 to num2.
On Line 3, we have assigned the value of num1 to num3. Now the value which was with the num1 variable will be copied to the num3 variable i.e. now the value of both num1 and num3 will be the same (10).
On Line 4, we have updated the value of variable num1 i.e. you can change the value of the variable according to your use at any time. By the way, the meaning of variable in Hindi is also “variable”.
In the last line, the values of all three variables num1 (20), num2 (20) and num3 (10) are assigned to the sum variable. If you print the sum variable, then 50 will be printed in the output.
Difference between Variable and Identifier
Some students get a bit confused about variable and identifier whether these two are the same thing or different.
To explain this, I give an example such as “Mango is a fruit but not every fruit is a mango” Just like “Variable is an identifier but not every identifier is a variable”.
Identifier is a name which we give to any variable, array, function, structure and other c programming entity but variable is just memory location name where we store our data.
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at firstname.lastname@example.org.