0% found this document useful (0 votes)
12 views8 pages

PSPC Unit III A Storage Classes in C

The document provides an overview of storage classes in C programming, detailing their scope and lifetime. It covers four main storage classes: auto, static, register, and extern, explaining their characteristics and usage with examples. Additionally, it discusses the memory storage layout and how to manage storage classes across multiple files.

Uploaded by

gdrivee515
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)
12 views8 pages

PSPC Unit III A Storage Classes in C

The document provides an overview of storage classes in C programming, detailing their scope and lifetime. It covers four main storage classes: auto, static, register, and extern, explaining their characteristics and usage with examples. Additionally, it discusses the memory storage layout and how to manage storage classes across multiple files.

Uploaded by

gdrivee515
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/ 8

10/12/2023

Dr. Dileep Kumar Singh


Head, JLU-SOET
[email protected]

Problem Solving and Program


Design using C

Problem Solving and Program Design using C

UNIT –III

B Tech/B Tech (Hons.) CSE – 1st Sem.

Storage Classes in C

Variable Storage Classes

• Storage class of a variable determines its:


– Scope
– Lifetime

• C storage classes are:


– auto
– static
– register
– extern

Dr. Dileep Kumar Singh 1


10/12/2023

Scope and Storage:


C variable v has scope and storage attributes
The storage attribute states, how long v lives
The scope attributes states, where v is visible
 Storage attributes can be static, auto, or register
 Scope attributes can be local, global, or external:
Local means, v is only visible inside the current,
innermost scope, independent of storage attribute;
e.g. there are local static variables in C
Global means, v is visible in the whole compilation
unit, from the line of declaration to the end of file
External means, v is visible in this and in other
compilation units, and its storage attribute is static
4

auto Storage Class

• auto is the default storage class for a variable defined


inside a function body or a statement block
• The auto prefix is optional; i.e. any locally declared
variable is automatically auto  unless specifically
defined to be static
Example:
{
auto double x; /* Same as: double x */
int num; /* Same as: auto int num; */
. . .
}
5

• Storage for an auto variable is:


– Allocated when control enters the variable’s
containing block
– Released when control leaves its containing block

Example:
{ enter block
int x; storage created for x

{ enter block
int y = 1; storage created for y
} leave block – storage for y released
} leave block – storage for x released
6

Dr. Dileep Kumar Singh 2


10/12/2023

• If an auto variable is defined but not initialized:


– Variable has an unknown value when control enters its
containing block
• If an auto variable is defined and initialized at the
same time:
– Variable is re-initialized each time control enters its
containing block
• An auto variable’s scope is limited to its containing
block (i.e., it is local to the block)

static Storage Class

•Storage for a static variable:


– Is allocated when execution begins
– Exists for as long as the program is running
•A static variable may be defined either inside or
outside a function’s body.
•The static prefix must be included
Example:
static double seed;

• If a static variable is defined but not initialized:


– Is set to zero (0) once, when storage is allocated
• If a static variable is simultaneously defined and
initialized:
– Is initialized once, when storage is allocated
• A static variable defined inside a function body is
visible only in its containing block
• A static variable defined outside a function body is
visible to all blocks which follow it in the current
compilation units
• If you wish it to be visible in other compilation units,
it must be declared extern
9

Dr. Dileep Kumar Singh 3


10/12/2023

Example:
#include <stdio.h>

void weird( int x )


{ // weird
static int y; /* Persistent */
if ( x == 0 )
printf( "%d\n", y );
else if ( x == 1 )
y = 100;
else if ( x == 2 )
y++;
} //end weird

int main (void)


{ // main
weird(1); /* Set y in weird to 100 */
weird(0); /* Will display 100 */
weird(2); /* Increment y in weird */
weird(0); /* Will display 101 */
return 0;
} // end main

10

10

register Storage Class

•The fastest storage resides within the CPU itself in


high-speed memory cells called registers
•The programmer can request the compiler to use a
CPU register for storage
Example:
register int k;
•The compiler can ignore the request, in which case the
storage class defaults to auto
•Some machines –e.g. stack architectures– have no
user visible register
11

11

extern Storage Class (single source file)

•extern is the default storage class for a variable


defined outside a function’s body

•Storage for an extern variable:


– Is allocated when execution begins
– Exists for as long as the program is running

12

12

Dr. Dileep Kumar Singh 4


10/12/2023

• If an extern variable is defined but not initialized:


– Is set to zero (0) once, when storage is allocated

• If an extern variable is simultaneously defined and


initialized:
– Is initialized once, when storage is allocated

• An extern variable is visible in all functions that


follow its definition (i.e., it is global)

13

13

Example:

#include <stdio.h>

float x = 1.5; /* Definition - extern class - global */

void disp (void)


{
printf("%f\n", x); /* Access global x */
}

int main (void)


{
printf("%f\n", x); /* Access global x */

disp();

return 0;
}

14

14

Memory Storage Layout

Where are auto, static, and extern variables stored?


Code Segment
Contains the program’s machine code
(Text Segment)

Data Segment Contains static data (e.g., static class, extern globals)

Stack Segment Contains temporary data (e.g., auto class)

free Unallocated memory that the stack and heap can use

Heap Segment Contains dynamically allocated data (e.g., via malloc)

15

15

Dr. Dileep Kumar Singh 5


10/12/2023

Storage Classes in Multiple Files

• Functions stored in a single source file can be divided


into separate source files.

• Variables defined in one source file can be accessed


from other source files via the extern storage class.

• An extern variable can be defined in one file only.


However, it may be declared from other files.

16

16

• An extern variable is defined exactly once in a file by


placing it outside all blocks.
– If an extern variable is not initialized at definition time
→ extern prefix must be omitted

– If an extern variable is initialized at definition time


→ extern prefix is optional

• An extern variable is declared in another file by using


the extern prefix.

Example:
extern int k; 17

17

Filename: ex1_A.c
#include <stdio.h>

double pi = 3.141592654; /* Definition of global variable here */

double deg_to_rad (double deg); /* Prototype */

int main (void)


{
printf("%f\n", deg_to_rad(45.0));
return 0;
}

Filename: ex1_B.c
extern double pi; /* Declaration allows it to be used here */

double deg_to_rad (double deg)


{
return deg * pi / 180;
}

18

18

Dr. Dileep Kumar Singh 6


10/12/2023

Storage Classes for Functions

•extern is the default storage class for a function.


•The extern prefix is usually omitted from the
function’s definition.

•An extern function can be invoked by any other


function in any file.

•If a function is invoked in one file but is defined in a


different file, then declare its prototype in the file
where the function is invoked.
19

19

Filename: ex2_A.c

#include <stdio.h>

int a = 123; /* Global in ex2_A.c */

void fun1 (int x); /* Prototype for fun1 in ex2_A.c */


void fun2 (void); /* Prototype for fun2 in ex2_B.c */

int main (void)


{
int a = 2; /* Local to main */

printf("1:main: a= %d\n", a);


fun1(a);
fun2();
printf("8:main: a= %d\n", a);
a = 99;
printf("9:main: a= %d\n", a);

return 0;
}
20

20

continued … this code is still part of ex2_A.c

void fun1 (int x)


{
printf("2:fun1: x= %d\n", x);
x = 21;
printf("3:fun1: a= %d\n", a);

{
int a; /* Local to this block in fun1 */
a = 5;
printf("4:fun1: a= %d\n", a);
}
printf("5:fun1: a= %d\n", a);
}

21

21

Dr. Dileep Kumar Singh 7


10/12/2023

Filename: ex2_B.c

#include <stdio.h>

extern int a; /* Declare variable that resides in ex2_A.c */

void fun2 (void)


{
printf("6:fun2: a= %d\n", a);
a = -99;
printf("7:fun2: a= %d\n", a);
}

22

22

Compile the source files using gcc:


gcc ex2_A.c ex2_B.c or gcc *.c

Actual output:
1:main: a= 2
2:fun1: x= 2
3:fun1: a= 123
4:fun1: a= 5
5:fun1: a= 123
6:fun2: a= 123
7:fun2: a= -99
8:main: a= 2
9:main: a= 99

23

23

THANKS

24

Dr. Dileep Kumar Singh 8

You might also like