Updated March 16, 2023
Introduction on Variables in C
Variables in the C language plays an important role. We can also say that variables are the backbone of many programming languages. Variables in C languages are used to store different forms of data. It acts as a memory card where it saves all the data and used it during program execution. There are different types of variables in C; according to their types, the amount of memory or storage space it requires differs. As we said, variables in C are storage used to hold the value. Data that variables can be different like int, float, char, double, etc. All the code or program depends on the variables as it describes the type of data for execution.
In this article, we are going to see how variables play an important role in C, how to initialize variables, how to declare, etc.
Rules for Defining Variables in C
- Variables in C must not start with the number; else, the Variable will not be valid. For example (1 string is not a valid variable).
- Blank space between variables is not allowed. For example, (string one is not valid, string_one is a valid variable).
- Keywords are not allowed to define as a variable. For example, (for is not a valid variable as it is used as a keyword in C language).
- As C is a case sensitive language, upper and lower cases are considered as a different variable. For Example (NUMBER and number will be treated as two different variables in C).
- Variable names can be a combination of string, digits, and special characters like underscores (_).
How to Work?
- While declaring variables, it tells compilers the type of data it holds.
- Variables tell compilers the name of the variables that are being used in the program.
- As variables specify storage, compilers do not have to worry about the variables’ memory location until they are declared.
How to Declare?
Variables should be declared first before the program as it plays an important role.
The syntax for variables declaration is as follows.
data_type variable_name;
where,
- data_type: Indicates types of data it stores. Data types can be int, float, char, double, long int, etc.
- variable_name: Indicates the name of the variable. It can be anything other than the keyword.
For example
- int a;
- int a, b, c;
For example, 1, int is a data type, and a is a variable name. In the second example, we have declared three variables, a, b, and c.
After variables are declared, the space for those variables has been assigned as it will be used for the program.
Program to Illustrate the Declaration of Variables in C
#include<stdio.h>
#include<conio.h>
int main()
{
int m, n;
m = 2;
n = 3;
z = m + n;
printf("Sum of two numbers is: %d \n", z);
return 0;
}
How to Initialize?
Initializing variables in C means allocating values to variables directly while declaring it. The syntax for initializing variables are as follows:
data_type variable_name = value;
For example
- int a = 10;
- int a = 5, b = 8;
In example 1, variable a is created and initialized with the value 10. For example, 2 two variables, a and b, are created allocated values 5 and 8, respectively.
Program to illustrate initialization of variables in C.
#include<stdio.h>
#include<conio.h>
int main()
{
int m = 2, n = 3;
z = m + n;
printf("Sum of two numbers is: %d \n", z);
return 0;
}
Types of Variables
There are 5 types of variables which are as follows:
- Local variables
- Global variables
- Static variables
- Automatic variables
- External variables
1. Local Variables
Variables that are declared inside the functions are called local variable. Local variables must be declared before use. Only local functions can change the value of variables.
Example
int main()
{
int m =10; //local variable
}
2. Global Variables
Variables that are declared outside the functions are called global variables. Any functions can change the value of variables.
Example
int n = 6; //global variable
int main()
{
int m =10; //local variable
}
3. Static Variables
variables that are declared with the static keyword are called static variables.
int main()
{
int m =10; //local variable
static n = 6; //static variable
}
4. Automatic Variables
all the variables that are declared inside the functions are default considered as automatic variables. Automatic variables can be declared using the auto keyword.
int main()
{
int m =10; //local variable (Automatic variable)
auto n = 6; //automatic variable
}
5. External Variables
External variables are declared using the extern keyword. The variables with the extern keyword can be used in multiple C source files.
extern m =10; //external variable
Conclusion – Variables in C
In this article, we have seen how to declare, initialize, along with their syntax and program to understand how to implement them. Also, we have seen types of it and rules for defining them. I hope you will find this article helpful.
Recommended Articles
This is a guide to Variables in C. Here we discuss how to initialize variables, how to declare along with their syntax and program to understand how to implement them. You can also go through our other suggested articles –