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

Storage class specifiers (1)

The document explains storage class specifiers in C, detailing their purpose, syntax, and types: auto, extern, static, and register. Each specifier has unique characteristics regarding storage location, initial value, scope, and lifetime of variables. Examples are provided to illustrate the usage and behavior of each storage class in C programming.

Uploaded by

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

Storage class specifiers (1)

The document explains storage class specifiers in C, detailing their purpose, syntax, and types: auto, extern, static, and register. Each specifier has unique characteristics regarding storage location, initial value, scope, and lifetime of variables. Examples are provided to illustrate the usage and behavior of each storage class in C programming.

Uploaded by

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

STORAGE CLASS SPECIFIERS

Storage class specifiers in C language tells the compiler where to store a


variable, how to store the variable, what is the initial value of the variable and
life time of the variable.

SYNTAX:
storage_specifier data_type variable _name;

extern int age;

TYPES OF STORAGE CLASS SPECIFIERS IN C:

1. auto
2. extern
3. static
4. register

Storage
Specifier Description
Storage place: CPU Memory
Initial/default value: Garbage value
Scope: local
Auto Life: Within the function only.
Storage place: CPU memory
Initial/default value: Zero
Scope: Global
Life: Till the end of the main
program. Variable definition might be
Extern anywhere in the C program.
Storage place: CPU memory
Initial/default value: Zero
Scope: local
Life: Retains the value of the variable
Static between different function calls.
Storage place: Register memory
Initial/default value: Garbage value
Scope: local
Register Life: Within the function only.

● For faster access of a variable, it is better to go for register specifiers rather


than auto specifiers.
● Because, register variables are stored in register memory whereas auto
variables are stored in main CPU memory.
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).
● 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 resides.
● They are assigned a garbage value by default whenever they are
declared.

Example:

#include <stdio.h> }
int main( ) printf ( "\t %d ",j);
{ }
auto int j = 1; printf( "%d\n", j);
{ }
auto int j= 2; Output:
{ 321
auto int j = 3;
printf ( " %d ", j);

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.

Example:

#include<stdio.h>

int x = 10 ;

int main( )

extern int y;

printf("The value of x is %d \n",x);

printf("The value of y is %d",y);

return 0;

int y=50;

Output:

The value of x is 10

The value of y is 50
static:

● This storage class is used to declare static variables which are popularly
used while writing programs in C language.
● Static variables have a 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.
● 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.
● Static variables retain the value of the variable between different function
calls.

Example:

#include <stdio.h>

void next(void);

static int counter = 7; /* global variable */

main() {

while(counter<10) {

next();

counter++; }

return 0;}

void next( void ) { /* function definition */

static int iteration = 13; /* local static variable */

iteration ++;

printf("iteration=%d and counter= %d\n", iteration, counter);}

Output:

iteration=14 and counter= 7


iteration=15 and counter= 8

iteration=16 and counter= 9

register:

● This storage class declares register variables which 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 register is not available, these are then stored in the memory
only. Usually 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.
● The address of a register variable cannot be obtained using pointers.

Example:

#include <stdio.h>

int main()

register int i;

int arr[5];// declaring array

arr[0] = 10;// Initializing array

arr[1] = 20;

arr[2] = 30;

arr[3] = 40;

arr[4] = 50;
for (i=0;i<5;i++)

// Accessing each variable

printf("value of arr[%d] is %d \n", i, arr[i]);

return 0;

Output:

value of arr[0] is 10

value of arr[1] is 20

value of arr[2] is 30

value of arr[3] is 40

value of arr[4] is 50

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