0% found this document useful (0 votes)
17 views10 pages

Storage Classes

The document discusses the four types of storage classes in C language: Auto, Static, Extern, and Register. Each storage class specifies the scope and lifetime of variables, with Auto being temporary, Static retaining values across function calls, Extern linking variables from other files, and Register optimizing variable storage in CPU registers. The document provides details on the characteristics and behaviors of each storage class along with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Storage Classes

The document discusses the four types of storage classes in C language: Auto, Static, Extern, and Register. Each storage class specifies the scope and lifetime of variables, with Auto being temporary, Static retaining values across function calls, Extern linking variables from other files, and Register optimizing variable storage in CPU registers. The document provides details on the characteristics and behaviors of each storage class along with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit III

Arrays & Structures

Storage classes
Storage classes

C language support 4 types of storage classes and these are used to specify the
scope of different variables defined with in function blocks and program.

 Auto
 Static
 Extern
 Register

2
Auto

i. Auto
 The variables without any storage class specification.
 These variables are given only temporary memory space and after the execution, all the automatic variables
will get disposed.
 Automatic variables are allocated memory automatically at runtime.
 The visibility of the automatic variables is limited to the block in which they are defined.
 The scope of the automatic variables is limited to the block in which they are defined.
 The automatic variables are initialized to garbage by default.
 The memory assigned to automatic variables gets freed upon exiting from the block.

Syntax: storage _class_type data_type var1, var2,…,varn;

3
Auto

Output

4
Static

 The variables defined as static specifier can hold their value between the multiple function calls.

 Static local variables are visible only to the function or the block in which they are defined.

 A same static variable can be declared many times but can be assigned at only one time.

 Default initial value of the static integral variable is 0 otherwise null.

 The visibility of the static global variable is limited to the file in which it has declared.

The keyword used to define static variable is static

5
Static

 During the first function call, the value of c is initialized to 1. Its


Output value is increased by 5. Now, the value of c is 6, which is printed on
the screen.

 During the second function call, c is not initialized to 1 again. It's


because c is a static variable. The value c is increased by 5. Now, its
value will be 11, which is printed on the screen.
6
Extern

 The external storage class is used to tell the compiler that the variable defined as extern is declared with an
external linkage elsewhere in the program.

 The variables declared as extern are not allocated any memory. It is only declaration and intended to specify
that the variable is declared elsewhere in the program.

 The default initial value of external integral type is 0 otherwise null.

 We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any
block or method.

 An external variable can be declared many times but can be initialized at only once.

 If a variable is declared as external then the compiler searches for that variable to be initialized somewhere in
the program which may be extern or static. If it is not, then the compiler will show an error.

7
Extern

output:

x in file1=10
x in file2=10

In the program, we have used two files-File1 and File2. File 1


has declared a global variable x. File1 also includes File2
which has a print function that uses the external variable x to
print its value on the screen.

8
Register

 The register storage class is used to define local variables that should be stored in a register instead of RAM.
 Since the variable is stored in a CPU register, the maximum size of the variable is equal to the register size.
 The register keyword is used to declare register variables. Register variables were supposed to be faster than local
variables.
 However, modern compilers are very good at code optimization, and there is a rare chance that using register
variables will make your program faster.

9
Register

OUTPUT:

10

You might also like