0% found this document useful (0 votes)
28 views21 pages

U4 - 1 - Storage Class

The document discusses different storage classes in C programming language including automatic, external, static, and register. It provides details about the scope, lifetime, memory location and initial value of variables for each storage class. The key differences between static and automatic (default) storage classes are highlighted such as static variables retaining their value between function calls while automatic variables are reinitialized each time.

Uploaded by

mit23cs
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)
28 views21 pages

U4 - 1 - Storage Class

The document discusses different storage classes in C programming language including automatic, external, static, and register. It provides details about the scope, lifetime, memory location and initial value of variables for each storage class. The key differences between static and automatic (default) storage classes are highlighted such as static variables retaining their value between function calls while automatic variables are reinitialized each time.

Uploaded by

mit23cs
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/ 21

UNIT IV

Topics to be Covered:
Storage class
Structure
 Features of structures
 Declaration and initialization of structures
 Array of structures
 Pointer to structure
 Structure and functions
Typedef
bit fields
enumerated data types
Union.

11/27/2023 BVL_Computer Centre, MIT Campus 1


Scope visibility and lifetime of a variable
• The scope of a variable in C is the block or the region in the
program where a variable is declared, defined, and used.
• Outside this region, we cannot access the variable and it is
treated as an undeclared identifier
• When speaking about scope, the term variable refers to all C
data types: simple variables, arrays, structure, pointers, and
so forth.
• The life time of variable refers that how long the variable
persists in memory, or when the variable's storage is allocated
and de-allocated.
• Depending upon the scope and lifetime of variable, C has its
four storage classes for variables used in any functions.
• Automatic variables
• External variables
• Static variables
• Register variables

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 2


Scope visibility and lifetime of a variable –
Cont’d
• The variables may broadly categorized , depending
upon the place of their declaration as:
• Internal(local)
• External(global)
• The internal(local) variables:
• Variables which are declared inside the function.
• They are not accessible outside the function.
• The external(global) variables:
• Variables which are declared outside the function.
• They are accessible to the entire program.

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 3


Scope visibility and lifetime of a variable –
Cont’d
• Local Variables
• Variables that are declared inside a function or block are called local
variables.
• They can be used only by statements that are inside that function or
block of code.
• Local variables are not known to functions outside their own.

Output:

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 4


Local variables are not known to
functions outside their own.

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 5


Scope visibility and lifetime of a variable –
Cont’d
• Global Variables
• Global variables are defined outside a function, usually on top
of the program.
• Global variables hold their values throughout the lifetime of
our program and they can be accessed inside any of the
functions defined for the program.
• A global variable can be accessed by any function.
• That is, a global variable is available for use throughout your
entire program after its declaration.

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 6


Scope visibility and lifetime of a variable –
Cont’d
Global variable

Output:

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 7


Scope visibility and lifetime of a variable –
Cont’d
• A program can have same name for local and global
variables but the value of local variable inside a function
will take preference.

Output:

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 8


Storage Classes in C
• Storage classes in C are used to determine the
lifetime, visibility, memory location, and initial value
of a variable.
• There are four types of storage classes in C
•Automatic
•External
•Static
•Register

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 9


Storage Classes in C
(Auto Storage Class)
• auto keyword refers to an automatic variable.
• By default, all local variables are automatic variables.
• The scope of an auto variable is within the function block and cannot be
accessed outside the function.
• Auto variables are declared at the beginning of the function.
• Once the function called the memory is allocated, and when the controls come
outside the function, the memory of auto variables is de-allocated.
• By default, the initial value of auto variables is garbage values with no
meaning.
• Syntax :
int a; //by default, it is an auto variable.
float b;
• This is similar to prefixing the keyword auto before the data types.
auto int a;
auto float b;

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 10


Storage Classes in C
(Auto Storage Class)

Output:

Garbage Value

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 11


Storage Classes in C
(Auto Storage Class)

Output:

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 12


Storage Classes in C
(Static Storage Class)
• Static variables are declared with the keyword static.
• The static variable's lifetime is throughout the program run-time.
• Static variables must be initialized before the program execution;
otherwise, the initial default value will be zero.
• The static variable declared inside the function cannot be accessed
outside the function.
• Static variables can be declared globally and accessed throughout the
program.
• Suppose the static variables are declared inside the function. Once the
function is called, a static variable's memory is allocated, and it won't
be de-allocated when the control comes out of the function. Therefore,
the current value of a static variable can be accessed in the following
function call.
• Syntax :
static data-type variable_name = value_initialization;

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 13


Storage Classes in C
(Static Storage Class)

Output:
Output:

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 14


Difference between Static and Auto Variable
Static Variable Auto Variable
We have to specify the storage class to
This is the default storage class.
make a variable static.

Default value is 0. Default value is garbage.

Static Variable is having local as well as Auto Variable is having block or local
file scope. scope.

Retains its value till the control remains


Retains its value between different
in the block in which the variable is
function calls. It holds its last value.
declared.

This variable should be compiled by Auto variable will compile by the


compiler first. compiler after the static variable.

Example: static int i; Example: int i;

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 15


Storage Classes in C
(Extern Storage Class)
• External variables are declared with the keyword extern.
• Variables of this storage class are Global variables and the scope of
the variable is throughout the program.
• We write extern keyword before a variable to tell the compiler that
this variable is declared somewhere else.
• Basically, by writing the extern keyword before any variable tells us
that this variable is a global variable declared in some other program
file.
• External variables share the variables among the multiple C files.
• External variables are stored in the memory, and their initial default
value is assigned to zero.
• The values assigned to the external variables can be changed in
another program block.
• Syntax :
extern data_type variable_name;

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 16


Storage Classes in C
(Extern Storage Class)
When extern not used

When extern is used:

Output:

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 17


Storage Classes in C
(Extern
extFile1.c
Storage Class)
Global variable

Output:

extFile2.c

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 18


Storage Classes in C
(Register Storage Class)
• Variable declared with the keyword register are register variables.
• The scope of register variables is within the function block where it is
defined.
• Register variables are similar to auto variables in C. however; they are
stored in the CPU register. Unlike the auto variables stored in the memory
• The frequently accessed variables are stored in the CPU register since the
access time is less and makes our program run faster.
• Sometimes, when all the CPU registers are busy, the register variables are
automatically stored in the memory, which is identified as an auto variable.
• Syntax :
register data_type variable_name;
11/27/2023 BVL_Computer Centre, Madras Institute of Technology 19
Storage Classes in C
(Register Storage Class)
Output:

• In the above example, the variable x is defined as a


register variable and is used as the loop counter in a for a
loop.
• Because x is defined as a register variable, it will be
stored in a register, and the program should run faster as
it will not be required to access memory as often.

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 20


Storage Classes in C
Storage Storage Default Scope Lifetime
Classes Place Value
auto RAM Garbage Local Within function
Value
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 Local Within the function


Value

11/27/2023 BVL_Computer Centre, Madras Institute of Technology 21

You might also like