
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain Lifetime of a variable in C language.
Storage classes specify the scope, lifetime and binding of variables.
To fully define a variable, one needs to mention not only its ‘type’ but also its storage class.
A variable name identifies some physical location within computer memory, where a collection of bits are allocated for storing values of variable.
Storage class tells us the following factors −
- Where the variable is stored (in memory or cpu register)?
- What will be the initial value of variable, if nothing is initialized?
- What is the scope of variable (where it can be accessed)?
- What is the life of a variable?
Lifetime
The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between allocation and deallocation of memory).
In C language, a variable can have automatic, static or dynamic lifetime.
- Automatic − A variable with automatic lifetime are created. Every time, their declaration is encountered and destroyed. Also, their blocks are exited.
- Static − A variable is created when the declaration is executed for the first time. It is destroyed when the execution stops/terminates.
- Dynamic − The variables memory is allocated and deallocated through memory management functions.
Storage Classes
There are four storage classes in C language −
Storage Class | Storage Area | Default initial value | Lifetime | Scope | Keyword |
---|---|---|---|---|---|
Automatic | Memory | Till control remains in block | Till control remains in block | Local | Auto |
Register | CPU register | Garbage value | Till control remains in block | Local | Register |
Static | Memory | Zero | Value in between function calls | Local | Static |
External | Memory | Garbage value | Throughout program execution | Global | Extern |
Example
Following is the C program for automatic storage class −
#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); }
Output
When the above program is executed, it produces the following output −
3 2 1
Example
Following is the C program for external storage class −
#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); }
Output
When the above program is executed, it produces the following output −
3 1
- Related Articles
- Explain scope of a variable in C language.
- Explain Binding of a variable in C language.
- Explain the accessing of structure variable in C language
- What is the lifetime of a static variable in a C++ function?
- Explain variable declaration and rules of variables in C language
- Explain the variable declaration, initialization and assignment in C language
- Explain C tokens in C Language
- Explain the history of C language?
- Explain the Format of C language
- Explain the concept of Sorting in C language
- Explain the concept of stack in C language
- Explain the concept of pointers in C language
- Explain array of pointers in C programming language
- Explain the array of structures in C language
- Explain Binary search in C language
