Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

What is Storage Class, Study notes of Computers and Information technologies

Storage class in C decides the part of storage to allocate memory for a variable, it also determines the scope of a variable. All variables defined in a C program get some physical location in memory where variable's value is stored. Memory and CPU registers are types of memory locations where a variable's value can be stored. The storage class of a variable in C determines the life time of the variable if this is 'global' or 'local'.

Typology: Study notes

2017/2018

Uploaded on 11/13/2018

hbansal1052000
hbansal1052000 🇮🇳

1 document

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
What is a Storage Class?
Storage class in C decides the part of storage to allocate memory for a
variable, it also determines the scope of a variable. All variables dened in
a C program get some physical location in memory where variable's value is
stored. Memory and CPU registers are types of memory locations where a
variable's value can be stored. The storage class of a variable in C
determines the life time of the variable if this is 'global' or 'local'. Along with
the life time of a variable, storage class also determines variable's storage
location (memory or registers), the scope (visibility level) of the variable,
and the initial value of the variable. There are four storage classes in C
those areautomatic, register, static, and external.
Storage Class Speciers
There are four storage class speciers in C as follows, typedef specier does
not reserve storage and is called a storage class specier only for syntactic
convenience. It is not a storage class specier in the common meaning.
auto
register
extern
static
typedef
These speciers tell the compiler how to store the subsequent variable. The
general form of a variable declaration that uses a storage class is shown
here:
storage_class_specifier data_type variable_name;
At most one storage class specier may be given in a declaration. If no
storage class specier is specied then following rules are used:
1. Variables declared inside a function are taken to be auto.
2. Functions declared within a function are taken to be extern.
3. Variables and functions declared outside a function are taken to
be static, with external linkage.
Variables and functions having external linkage are available to all les that
constitute a program. File scope variables and functions declared
as static (described shortly) have internal linkage. These are known only
within the le in which they are declared. Local variables have no linkage
and are therefore known only within their own block.
Types of Storage Classes
There are four storage classes in C they are as follows:
pf3
pf4
pf5

Partial preview of the text

Download What is Storage Class and more Study notes Computers and Information technologies in PDF only on Docsity!

What is a Storage Class?

Storage class in C decides the part of storage to allocate memory for a variable, it also determines the scope of a variable. All variables defined in a C program get some physical location in memory where variable's value is stored. Memory and CPU registers are types of memory locations where a variable's value can be stored. The storage class of a variable in C determines the life time of the variable if this is 'global' or 'local'. Along with the life time of a variable, storage class also determines variable's storage location (memory or registers), the scope (visibility level) of the variable, and the initial value of the variable. There are four storage classes in C those are automatic , register , static , and external.

Storage Class Specifiers

There are four storage class specifiers in C as follows, typedef specifier does not reserve storage and is called a storage class specifier only for syntactic convenience. It is not a storage class specifier in the common meaning. ■ auto ■ register ■ extern ■ static ■ typedef These specifiers tell the compiler how to store the subsequent variable. The general form of a variable declaration that uses a storage class is shown here: storage_class_specifier data_type variable_name; At most one storage class specifier may be given in a declaration. If no storage class specifier is specified then following rules are used:

  1. Variables declared inside a function are taken to be auto.
  2. Functions declared within a function are taken to be extern.
  3. Variables and functions declared outside a function are taken to be static, with external linkage. Variables and functions having external linkage are available to all files that constitute a program. File scope variables and functions declared as static (described shortly) have internal linkage. These are known only

within the file in which they are declared. Local variables have no linkage and are therefore known only within their own block.

Types of Storage Classes

There are four storage classes in C they are as follows:

  1. Automatic Storage Class
  2. Register Storage Class
  3. Static Storage Class
  4. External Storage Class Now, let us discuss these storage classes one by one.

1. Automatic Storage Class

A variable defined within a function or block with auto specifier belongs to automatic storage class. All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned. Variables having automatic storage class are local to the block which they are defined in, and get destroyed on exit from the block. The following C program demonstrates the visibility level of auto variables. #include <stdio.h> int main( ) { auto int i = 1; { auto int i = 2; { auto int i = 3; printf ( " \n %d ", i); } printf ( "%d ", i); } printf( "%d \n ", i); } OUTPUT ====== 3 2 1

In above example program you see three definitions for variable i. Here, you may be thinking if there could be more than one variable with the same name. Yes, there could be if these variables are defined in different blocks. So, there will be no error here and the program will compile and execute successfully. The printf in the inner most block will print 3 and the variable i defined in the inner most block gets destroyed as soon as control

exits from the block. Now control comes to the second outer block and prints 2 then comes to the outer block and prints 1. Here, note that automatic variables must always be initialized properly, otherwise you are likely to get unexpected results because automatic variables are not given any initial value by the compiler.

2. Register Storage Class

void staticDemo() { static int i; { static int i = 1; printf("%d ", i); i++; } printf("%d \n ", i); i++; }

int main() { staticDemo(); staticDemo(); } OUTPUT ====== 1 0 2 1

■ When static specifier is applied to a global variable or a function then

compiler makes that variable or function known only to the file in which it is defined. A static global variable has internal linkage that means even though the variable is global; routines in other files have no knowledge of it and cannot access and alter its contents directly. The following C program defines one static global variable gInt and a static function staticDemo(), for the variable and function are defined static they cannot be used outside the file (translation unit) staticdemo.c.. /* staticdemo.c */ #include <stdio.h> static int gInt = 1; static void staticDemo() { static int i; printf("%d ", i); i++; printf("%d \n ", gInt); gInt++; }

int main() { staticDemo(); staticDemo(); } OUTPUT ======

0 1 1 2

Static variables have default initial value zero and initialized only once in their lifetime.

4. External Storage Class

The extern specifier gives the declared variable external storage class. The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program. To understand why this is important, it is necessary to understand the difference between a declaration and a definition. A declaration declares the name and type of a variable or function. A definition causes storage to be allocated for the variable or the body of the function to be defined. The same variable or function may have many declarations, but there can be only one definition for that variable or function. When extern specifier is used with a variable declaration then no storage is allocated to that variable and it is assumed that the variable has already been defined elsewhere in the program. When we use extern specifier the variable cannot be initialized because with extern specifier variable is declared, not defined. In the following sample C program if you remove extern int x; you will get

an error "Undeclared identifier 'x'" because variable x^ is defined later than it has been used in printf. In this example, the extern specifier tells the compiler that variable x has already been defined and it is declared here for compiler's information. #include <stdio.h> extern int x; int main() { printf("x: %d \n ", x); } int x = 10;