In this tutorial, we are going to learn about variables in the C programming language. Learning the concept of variables is also an important step toward learning programming in the C language. We will see some examples, to understand the concept of variables, and later on, you would be able to use variables in your C programs.
Variables in C
What are the variables in C?
At times, in our C programs, we might need to use some data, and certainly, we would require to store the data. So here, in order to store some data, we can make use of the variables in our C programs.
Well, we can understand the variables as containers for storing some data. You can say that the variables will hold some data(of some particular data type. You can understand data type as if it represents the type of data held by the variable).
The variables would have some name, which is an identifier, and now you might be familiar with the rules to name a variable(which are the rules for identifiers). If you are going to store age within a variable, you can give the variable name as age, right?
So, using some examples, you would surely get the concept of variables, and what is the need to use the variables.
Whenever you create a variable, you need to specify some information about that variable, like the type of data that you are going to store in the variable(do not worry about the datatypes, since it is a concept that you would soon go through), name of the variable, and you can also give the value that you want to assign to that variable(or you can assign some value to the variable afterward).
Let’s have a look at a simple example, through which, we can understand the concept of variables and the need of using variables.
#include <stdio.h>
int main()
{
int age = 30;
float salary = 102900.90;
return 0;
}
As you can see in the above example, we have created two variables, out of which, one is holding an integer, and another holds a floating point number. If you carefully observe the way we are creating a variable, you would see that first, we are declaring the type, which can be something like int, float, char, double, long, etc. After that, we have a simple name for the variable.
Remember that while you are deciding some name for the variable, make sure you follow the rules for naming some identifier, and also, give some useful name to it. For example, if you are creating a variable to store the age of some person, you can name the variable as age, instead of naming it as a, b, or c. This is not compulsory, and not going to make much difference for the program or the compiler, but it would make your program more readable, and more understandable. After that, we are assigning the required value to the variable, using the assignment operator.
In the previous example, we have directly assigned the value to the variable, but we can also create some variables, and store some values later(this is referred to as variable definition). Have a look at the below example, to understand the same.
#include <stdio.h>
int main()
{
int age = 30;
float salary;
salary = 102900.90;
return 0;
}
As you can see in the above program, we created the variable with the name salary first, and then, we are storing the data in the variable. So, you can use the variables as and when required in your C programs.
Also, when you are creating variables, be careful in cases of redefinition. Let’s have a look at a simple program, and then we will try to explain the scenario.
#include <stdio.h>
int main()
{
int age = 30;
float age = 30.20;
return 0;
}
As it can be seen in the above program, first, we declared the age variable as an int type variable, and in the next line, we are again creating a variable with the same name, but the datatype is float this time, and due to this, you can run into problems. If you try to run the above program, you would find that we are not allowed to do that.
Summary
In this tutorial, we learned about the variables in C. Variables are an important concept to be understood in C, and often in your C programs, you would need to create different variables, for storing some data.
When you would do some different programs, you would get comfortable creating and using variables in your C programs.
You can learn more concepts related to C language with us on GyaniPandit, and also, you can explore a lot of other courses, related to Python, Machine learning, and Data Science on GyaniPandit, and upskill yourself.
FAQs related to Variables in C
Q: What are the variables in C?
Ans: variables in C language can be considered as some containers for data storage. We are able to store data of some particular type in the variables.
Q: how to create variables in C?
Ans: To create variables in C, we just need to state the type of data it would hold, followed by the name of the variable, and then we can also assign some value to the variable.