The lifetime of a variable (also know as the storage duration of the variable) determines the duration for which the variable will stay valid in the memory before it is destroyed. The storage class of the variable specifies the lifetime.
It is important to note that the scope and the lifetime of a variable are different. You can find more information about this difference here.
In C, a variable can only have one of the following three storage durations:
The compiler only creates a static when it executes the variable’s declaration statement for the first time. The following snippet of code creates a static integer:
static int num = 0;
If the declaration statement is executed more than once, the compiler ignores all successive calls and does not change the variable’s value.
A static variable is stored in the data segment of the object file of a program.
A static variable remains valid until the program runs, even if the parent block of code has finished executing.
In the following snippet of code, a static variable is created in the memory when the declaration statement (line 5) executes for the first time, i.e., during the first function call. When the function ends, the static variable is not deleted from the memory because its lifetime is valid until the program is running.
When the function is executed for the second time, the declaration statement is ignored and the variable’s value remains unchanged. We can see this here:
#include<stdio.h>void incrementAndPrint() {// declaring and initialising a static variablestatic int num = 2;// incrementing in the numbernum = num + 2;//printing the static variableprintf("Number: %d \n", num);}void main() {// calling the function twice to observe the behaviour of static variableincrementAndPrint();incrementAndPrint();}
An automatic variable (also known as a local variable) is created when the program enters its parent block of code. The following code snippet creates a local variable inside its parent function:
int num = 0;
If the declaration statement is repeated, the compiler does not ignore it and throws an error.
A local variable is stored in the function call stack of a program.
An automatic variable remains valid until its parent block of code is running. The variable is removed from the memory when the parent block stops executing.
A variable is created inside a function in the next snippet of code. This function is called twice.
The following steps will take place with every function call:
This is why the program prints the value 4 twice (once in each function call). Take a look at the code below.
#include<stdio.h>void incrementAndPrint() {// declaring and initialising a variableint num = 2;// incrementing in the numbernum = num + 2;//printing the variableprintf("Number: %d \n", num);}void main() {// calling the function twice to observe the behaviour of the variableincrementAndPrint();incrementAndPrint();}
A dynamic variable is created when we allocate the variable’s memory. We use a pointer and functions like calloc
and malloc
to allocate the dynamic memory to a variable. Below is an example of a declaration statement for a dynamic variable:
int* ptr = (int*) malloc(sizeof(int));
If we want to create another dynamic variable, we will have to use another pointer. If the same pointer is used, an error is generated.
A dynamic variable is stored on the heap of the program.
A dynamic variable is only removed from the heap when deallocating it using the free
function. If we do not deallocate the memory and the program ends, it is called a memory leak.
The following snippet of code creates a dynamic variable using the malloc
function, prints its original and the incremented value, and deallocates the memory:
#include<stdio.h>#include<stdlib.h>int main() {// creating a pointer and allocating dynamic memoryint* ptr = (int*) malloc(sizeof(int));// setting the variable's value*ptr = 10;// printing the variable's valueprintf("Value of *ptr: %d \n", *ptr);// incrementing the variable's value(*ptr)++;// printing the incremented valueprintf("Value of *ptr: %d \n", *ptr);// deallocating the dynamic memory allocated to the pointerfree(ptr);}
Free Resources