.

C Programming Storage Classes Assignment Help

Storage Classes:

Storage classes are categories of memory allocation for variable or function which includes the location where it resides in memory, initial value, availability and lifespan. There are four categories such as :

Storage Classes

Memory Location

Initial Value

Availability

Lifespan

auto

RAM

Garbage Value

Local

Within the block

extern

RAM

Zero

Global

Till the end of the main program Maybe declared anywhere in the program

static

RAM

Zero

Local

Till the end of the main program, Retains value between multiple functions call

register

Register

Garbage Value

Local

Within the block

Auto:

Default storage classNameis auto for all local variables or functions.


C Programming Storage Classes Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com


Examplefor auto.

int sub(void)

int a=10;

auto b=5;

return a+b;

The above example contains two auto storage classNamevariables a and b.

Example for auto:

#include <stdio.h>

int main( )

auto int a = 10;

auto int b= 20;

auto int c = 30;

printf ( {" %d "}, a);

printf ( {"\t %d "},b);

printf( {"%d\n"}, c);

Output:

10 20 30

Program for Extern:

1st file : main.c

#include <stdio.h>

int count ;

extern void write_extern();

main()

count = 1;

write_extern();

2nd file:count.c

#include <stdio.h>

extern int count;

void write_extern(void)

printf({"count is %d\n"}, count);

Compilation of files main.c and count.c

$gcc main.c support.c

Run the program a.out and Output will be:

Count is 1

Program for Static:

#include <stdio.h>

{'/* function declaration */'}

void add(void);

static int a = 5; /* global variable */

main()

add();

return 0;

{'/* function definition */'}

void add( void )

static int b = 10; /* local static variable */

c=a+b;

printf("sum of a & b is %d", c);

Output:

The above program produces the output as

Sum of a & b is 15

.