Variables in C Programming

Variables in C Programming post thumbnail image

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.

Variables in C Programming
Variables in C Programming

You can also read: Prime Number Program in C Language

Variables in C Programming Naming Rules in C Language

  • Variable name में सिर्फ alphanumeric characters ( a-z , A-Z , 0-9 ) और underscore ( _ ) का use कर सकते हैं.
  • The first character of the variable name can only be alphabet( a-z , A-Z ) or underscore ( _ ).
  • There should not be space in the variable name and you cannot use keywords instead of variable name.
  • Variable names are case sensitive like num and Num will be considered as two different variable names.
  • Variable name should be meaningful and in lowercase it is considered good programming practice.

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:

data_type variable_name;

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.

Note

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;

Explanation:

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 praveen171086@gmail.com.

You can also visit my Hindi YouTube Channel.

mm
Avinash Singh

I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E – Vision Technocraft Pvt Ltd.

Leave a Reply

Your email address will not be published.

Related Post