C Variables
A variable in C language is the name associated with some memory location to store data of different types. There are many types of variables in C depending on the scope, storage class, lifetime, type of data they store, etc. A variable is the basic building block of a C program that can be used in expressions as a substitute in place of the value it stores.
In C programming, a variable is a named storage location in memory that holds a value which can be modified during program execution. Each variable in C has a specific type that determines the kind of data it can hold, such as integers, floating-point numbers, or characters.
C Variable Syntax
The syntax to declare a variable in C specifies the name and the type of the variable.
data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple variable
- data_type: Type of data that a variable can store.
- variable_name: Name of the variable given by the user.
- value: value assigned to the variable by the user.
Example
int var; // integer variable
char a; // character variable
float ff; // float variables
There are 3 aspects of defining a variable:
- Variable Declaration
- Variable Definition
- Variable Initialization
1. C Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with the given name and data type.When the variable is declared, an entry in symbol table is created and memory will be allocated at the time of initialization of the variable.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and some value to it. A defined variable will contain some random garbage value till it is not initialized.
3. C Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful value to the variable when creating the variable.
Rule for naming Variable in C:
We give a variable a meaningful name when we create it. Here are the rules that we must follow when naming it:
1. The name of the variable must not begin with a digit.
2. A variable name can consist of digits, alphabets, and even special symbols such as an underscore ( _ ).
3. A variable name must not have any keywords, for instance, float, int, etc.
4. There must be no spaces or blanks in the variable name.
5. The C language treats lowercase and uppercase very differently, as it is case sensitive. Usually, we keep the name of the variable in the lower case.
C Variable Types
The C variables can be classified into the following types:
- Local Variables
- Global Variables
- Static Variables
- Automatic Variables
- Extern Variables
- Register Variables
1. Local Variables in C
A Local variable in C is a variable that is declared inside a function or a block of code. Its scope is limited to the block or function in which it is declared.
2. Global Variables in C
A Global variable in C is a variable that is declared outside the function or a block of code. Its scope is the whole program i.e. we can access the global variable anywhere in the C program after it is declared.
3. Static Variables in C
A static variable in c is a variable that is defined using the static keyword. It can be defined only once in a C program and its scope depends upon the region where it is declared (can be global or local).
4. Automatic Variable in C
All the local variables are automatic variables by default. They are also known as auto variables.
Their scope is local and their lifetime is till the end of the block. If we need, we can use the auto keyword to define the auto variables.
5. External Variables in C
External variables in C can be shared between multiple C files. We can declare an external variable using the extra keyword.
6. Register Variables in C
Register variables in C are those variables that are stored in the CPU register instead of the conventional storage place like RAM. Their scope is local and exists till the end of the block or a function.