Lifetime of a Variable in C
What is a Variable?
A variable is a named location in computer memory that stores a value, such as a number, a character, or a data structure. In programming, variables are used to hold values that can be modified or used in computations.
To use a variable, you must first declare it, which tells the compiler the type of data that the variable will hold and reserves a portion of memory to store the variable.
The syntax for declaring a variable in C is as follows:
Syntax:
data_type variable_name;
Here, data_type is the type of data that the variable will hold, such as int for integer values, float for floating-point values, or char for character values. variable_name is the name that you give to the variable, which can be any valid identifier.
After declaring a variable, you can assign a value to it using the assignment operator =.
Example:
int x; // declare an integer variable named x
x = 42; // assign the value 42 to x
You can also declare and assign a value to a variable in a single statement, like this:
int y = 10; // declare an integer variable named y and assign the value 10 to it
Once you have assigned a value to a variable, you can use the variable in expressions and computations.
Example:
int z = x + y; // add the values of x and y and assign the result to a new variable named z
In summary, a variable is a named location in memory that can hold a value of a specific data type, and is declared using the syntax data_type variable_name;.
Once declared, a variable can be assigned a value and used in expressions and computations.
What is a lifetime of a variable?
In C, the lifetime of a variable refers to the duration for which the variable exists in memory and can be accessed by the program. The lifetime of a variable depends on its storage class, which determines where the variable is stored and how long it remains in memory.
There are four storage classes in C:
- Auto
- Static
- Register
- Extern
Auto variables
An auto variable is a local variable declared within a function or block with the keyword "auto". Auto variables have a lifetime that begins when the block or function is entered and ends when the block or function is exited. This means that the variable is created when the function or block is entered, and destroyed when the function or block is exited.
Example:
Here an example of auto variable in C
#include <stdio.h>
int main() {
for (auto inti = 0; i<= 5; i++) { // declare an auto variable i and initialize it to 0
printf("i = %d\n", i); // print the value of i
}
return 0;
}
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
Static variables
A static variable is a local variable declared with the keyword "static". Unlike auto variables, the lifetime of a static variable is not tied to the function or block in which it is declared.
Instead, a static variable is created and initialized only once, when the program starts running.
The variable remains in memory for the entire duration of the program and retains its value between function calls.
Example:
#include <stdio.h>
void foo() {
static int count = 0; // declare a static variable named count and initialize it to 0
count++; // increment the value of count
printf("Function has been called %d times.\n", count); // print the current value of count
}
int main() {
foo(); // call the function foo
foo(); // call the function foo again
foo(); // call the function foo a third time
return 0;
}
Output:
Function has been called 1 times.
Function has been called 2 times.
Function has been called 3 times.
Register variables
A register variable is a local variable declared with the keyword "register". The compiler is encouraged to store these variables in processor registers for faster access.
The lifetime of a register variable is the same as an auto variable, that is, it begins when the function or block is entered and ends when the function or block is exited.
Extern variables
An extern variable is a global variable declared with the keyword "extern". Extern variables are not defined within the file where they are declared; instead, they are defined in another file.
The lifetime of an extern variable is the same as the program's lifetime. It is created when the program starts running and destroyed when the program terminates.
In summary, the lifetime of a variable in C is determined by its storage class. The duration can range from the lifespan of a function or block for auto and register variables to the entire lifespan of the program for extern variables.