0% found this document useful (0 votes)
38 views4 pages

C Storage Classes Explained

Every variable in C programming has a data type and storage class that determine its scope and lifetime. There are 4 storage classes: automatic, external, static, and register. Automatic variables are local to the function they are declared in and cease to exist when the function exits. External/global variables are declared outside of functions and can be accessed by any function. Static variables persist for the duration of the program while register variables provide no optimization benefit for modern compilers.

Uploaded by

p.prav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

C Storage Classes Explained

Every variable in C programming has a data type and storage class that determine its scope and lifetime. There are 4 storage classes: automatic, external, static, and register. Automatic variables are local to the function they are declared in and cease to exist when the function exits. External/global variables are declared outside of functions and can be accessed by any function. Static variables persist for the duration of the program while register variables provide no optimization benefit for modern compilers.

Uploaded by

p.prav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Every variable in C programming has two properties: type and

storage class.

Type refers to the data type of a variable. And, storage class


determines the scope and lifetime of a variable.

There are 4 types of storage class:

1. automatic
2. external
3. static
4. register

Local Variable
The variables declared inside the function are automatic or local
variables.

The local variables exist only inside the function in which it is


declared. When the function exits, the local variables are
destroyed.

int main() {

int n; // n is a local varible to main() function

... .. ...

}
void func() {

int n1; // n1 is local to func() fucntion

void main()

int detail;

or

auto int detail; //Both are same

Global Variable
Variables that are declared outside of all functions are known as
external variables. External or global variables are accessible to
any function.

Suppose, a global variable is declared in  file1 . If you try to use that variable
in a different fi le  file2 , the compiler will complain. To solve this problem,
keyword  extern  is used in  file2 to indicate that the external variable is declared in
another fi le.
int number;

void main()

number=10;

fun1()

number=20;

fun2()

number=30;

Here the global variable number is available to all three functions.

Register Variable
The register keyword is used to declare register variables. Register variables
were supposed to be faster than local variables.

However, modern compilers are very good at code optimization and there is
a rare chance that using register variables will make your program faster.  

Unless you are working on embedded system where you know how to
optimize code for the given application, there is no use of register variables.

Static Variable
A static variable is declared by using keyword static. For example;

static int i;
The value of a static variable persists until the end of the program.

You might also like