0% found this document useful (0 votes)
64 views

Storage Classes in C

C has four storage classes - auto, extern, static, and register - that determine the scope, visibility, and lifetime of variables. Auto variables are local to the block they are declared in and do not retain their values outside the block. Extern variables are defined elsewhere and allow access between files. Static variables retain their value within a function and exist throughout program execution. Register variables attempt to store in processor registers for faster access but may be stored in memory.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Storage Classes in C

C has four storage classes - auto, extern, static, and register - that determine the scope, visibility, and lifetime of variables. Auto variables are local to the block they are declared in and do not retain their values outside the block. Extern variables are defined elsewhere and allow access between files. Static variables retain their value within a function and exist throughout program execution. Register variables attempt to store in processor registers for faster access but may be stored in memory.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Storage Classes in C

• C Storage Classes are used to describe the features of a variable/function.

• These features basically include the scope, visibility, and lifetime which help us to trace the

existence of a particular variable during the runtime of a program.

• C language uses 4 storage classes, namely:

1. auto

2. extern

3. static

4. register
Syntax

To specify the storage class for a variable, the following syntax is to be followed:

storage_class var_data_type var_name;


1. auto
• This is the default storage class for all the variables declared inside a function or a block. Hence, the

keyword auto is rarely used while writing programs in C language.

• Auto variables can be only accessed within the block/function they have been declared and not

outside them (which defines their scope). Of course, these can be accessed within nested blocks

within the parent block/function in which the auto variable was declared.

• However, they can be accessed outside their scope as well using the concept of pointers given here

by pointing to the very exact memory location where the variables reside. They are assigned a

garbage value by default whenever they are declared.


#include <stdio.h>
int main()
{
//declaration of automatic variables within main function
auto int m1 = 5; //declaration of automatic variable with the
keyword auto
int m2 = 10; //declaration of automatic variable without the
keyword auto
printf("The value of m1 : %d ", m1);
printf("\nThe value of m2 : %d ", m2);
return 0;
} output
The value of m1 : 5
The value of m2 : 10
2. extern
• Extern storage class simply tells us that the variable is defined elsewhere and not within the same
block where it is used. Basically, the value is assigned to it in a different block and this can be
overwritten/changed in a different block as well. So an extern variable is nothing but a global
variable initialized with a legal value where it is declared in order to be used elsewhere. It can be
accessed within any function/block.
• Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword
before its declaration/definition in any function/block. This basically signifies that we are not
initializing a new variable but instead, we are using/accessing the global variable only.
• The main purpose of using extern variables is that they can be accessed between two different
files which are part of a large program.
#include <stdio.h>

extern int num1 = 1;

extern int num2 = 2;

int main()

int num1 = 3;

int num2 = 4;

int add = num1 + num2; Output

printf("%d + %d = %d ", num1, num2, add); 3+4=7

return 0;

}
3. static
• This storage class is used to declare static variables which are popularly used while
writing programs in C language. Static variables have the property of preserving their
value even after they are out of their scope! Hence, static variables preserve the value
of their last use in their scope. So we can say that they are initialized only once and
exist till the termination of the program. Thus, no new memory is allocated because
they are not re-declared.
• Their scope is local to the function to which they were defined. Global static
variables can be accessed anywhere in the program. By default, they are assigned the
value 0 by the compiler.
#include <stdio.h>
void subfun();
int main()
{
subfun(); output
subfun(); st = 1
st = 2
subfun(); st = 3
return 0;
}
void subfun()
{
static int st = 1; //static variable declaration
printf("\nst = %d ", st);
st++;
}
4. register
• This storage class declares register variables that have the same functionality as that of the
auto variables. The only difference is that the compiler tries to store these variables in the
register of the microprocessor if a free register is available.
• This makes the use of register variables to be much faster than that of the variables stored
in the memory during the runtime of the program.
• If a free registration is not available, these are then stored in the memory only. Usually, a
few variables which are to be accessed very frequently in a program are declared with the
register keyword which improves the running time of the program.
• An important and interesting point to be noted here is that we cannot obtain the address of a
register variable using pointers.
#include <stdio.h>
int main()
{
int m1 = 5;
register int m2 = 10;
printf("The value of m1 : %d ",m1);
printf("\nThe value of m2 : %d ",m2);
return 0;
}

output
The value of m1 : 5
The value of m2 : 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy