Introduction
Fundamentals
C Flow Control
C Functions
C Arrays
Variables in C Language
In this tutorial, we will learn about variables and their naming rules. We will also learn about different-different types of variables in C Language.
The primary purpose of variables is to store data in memory for later use. Each variable in C has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
int age = 30;
In the above example, age is a variable, which can hold integer type of data. Currently 30 is stored in this variable.
1. Rules For Naming Variables:
- A variable name can only have alphabates (a-z, A-Z), numeric characters (0-9) and underscore (_).
- The first letter of a variable should be either a letter (a-z, A-Z) or underscore (_). So a valid variable name can't start with a numeric character.
- A keyword cannot be used as a variable name.
- As a good practice, we should always provide meaningful and short names to variables.
2. Examples of Valid and Invalid Variable Names:
Variable Name | Valid | Remarks |
---|---|---|
age | ✅ | Variable name can start with letter(a-z, A-Z) or underscore (_). |
age_* | ❌ | In variable name, no special characters allowed other than underscore (_). |
int | ❌ | Variable name should not be a C keyword. |
1age | ❌ | Variable Name should not start with any numeric character. |
age1 | ✅ | Variable name can have numberic characters in between or end. |
3. Type of Variables in C Language:
There are five types of variables in C Language, which are following:
1. Local Variable: A variable that is declared inside the function or a block is called local variable. Local Variables must be declared at the start of the block. A local variable must be initialized before it is used.
void functionX() {
int rollNo = 10; // local variable
}
2. Static Variable: A variable that is declared with the static keyword is called static variable. It retains its value between multiple function calls.
void function1() {
int = 20; // local variable
static int y = 5; // static variable
x = x + 1;
y = y + 1;
printf("%d,%d", x, y);
}
3. Automatic Variable: All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.
void main() {
int n = 10; // local variable (also automatic)
auto int m = 20; //automatic variable
}
4. Global Variable: A variable that is declared outside the function or block is called a global variable. A global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program. It must be declared at the start of the block.
int value = 20; // global variable
void function1() {
int x = 10; // local variable
}
5. External variable: We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.
extern int x = 10; //external variable (also global)