Home > Article > Backend Development > Explain the life cycle of variables in C language
The storage class specifies the scope, life cycle and binding of the variable.
To fully define a variable, it is necessary to mention not only its "type" but also its storage class.
A variable name identifies a physical location in computer memory where a set of bits is allocated to store the variable's value.
The storage class tells us the following factors -
The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between memory allocation and deallocation).
In C language, variables can have automatic, static or dynamic life cycle.
There are four storage classes in C language-
Storage Class | Storage Area | Default initial value | Life cycle | Scope | Keyword |
---|---|---|---|---|---|
Auto | Memory | Until control remains in the block | Until control remains in the block | Local | Automatic |
Registers | CPU Registers | Garbage Value | Until control remains in the block | Local | Register |
static | memory | zero | value between function calls | local | static |
External | Memory | Garbage value | Entire program execution | Global | External | tr>
The following is a C program for the automatic storage class-
Live Demo
#include<stdio.h> main ( ){ auto int i=1;{ auto int i=2;{ auto int i=3; printf ("%d",i) } printf("%d", i); } printf("%d", i); }
When executing the above program, the following output will be produced-
3 2 1
The following is a C program for the external storage class-
Live demonstration
#include<stdio.h> extern int i =1; /* this ‘i’ is available throughout program */ main ( ){ int i = 3; /* this ‘i' available only in main */ printf ("%d", i); fun ( ); } fun ( ) { printf ("%d", i); }
When executing the above program, the following output is produced -
3 1
The above is the detailed content of Explain the life cycle of variables in C language. For more information, please follow other related articles on the PHP Chinese website!