0% found this document useful (0 votes)
47 views

Storage Classes

The document discusses C storage classes and their properties. There are four main storage classes - auto, register, static, and extern. Auto variables are local to a block and deleted when the block ends. Register variables may be stored in CPU registers for faster access. Static variables retain their value between function calls. Extern allows other files to access global variables defined elsewhere.

Uploaded by

Akhilesh Wuna
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)
47 views

Storage Classes

The document discusses C storage classes and their properties. There are four main storage classes - auto, register, static, and extern. Auto variables are local to a block and deleted when the block ends. Register variables may be stored in CPU registers for faster access. Static variables retain their value between function calls. Extern allows other files to access global variables defined elsewhere.

Uploaded by

Akhilesh Wuna
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/ 16

Storage Classes


Properties of a variable: A variable in C language is associated with the
following properties: Property Meaning

Name : Name is used to refer to variable. This is a symbol using which the
value of the variable is accessed or modified. Data type Specifies what type of
value the variable can store

Value : The value that is stored in the variable. The value is not known unless
the variable is explicitly initialized.

Address :The address of the memory location where the variable is allocated
space

Scope :Specifies the region of the program from where variable can be
accessed.

 Visibility : Specifies the area of the program where variable is visible. Extent
Specifies how long variable is allocated space in memory.
Blocks: C is a block structured language. Blocks are delimited by { and }. Every block
can have its own local variables. Blocks can be defined wherever a C statement
could be used. No semi-colon is required after the closing brace of a block.
Scope :
The area of the program from where a variable can be accessed is called as scope of
the variable. The scope of a variable determines over what parts of the program a
variable is actually available for use (active). Scope of the variable depends on where
the variable is declared.
int g; /* scope of g is from this point to end of program */
main()
{
int x; /* scope of x is throughout main function */
....}
void sum()
{ int y; /* scope of y is throughout function sum */
....}

The scope of a variable may be either global or local.


 STORAGE CLASS SPECIFIERS 'Storage' refers to the scope of a
variable and memory allocated by compiler to store that
variable. Scope of a variable is the boundary within which a
variable can be used. Storage class defines the scope and
lifetime of a variable.

 From the point view of C compiler, a variable name identifies
physical location from a computer where variable is stored.
There are two memory locations in a computer system where
variables are stored as: Memory and CPU Registers.

 Functions of storage class:
1 To determine the location of a variable where it is stored?
2. Set initial value of a variable or if not specified then setting it
to default value.
3. Defining scope of a variable.
4. To determine the life of a variable.

 Syntax: [storage-class] data type variable [= value] ;

 Types of Storage Classes
1.Auto
 2.register
3. static
 4. extern

 auto or Automatic Storage
 Auto or Automatic Storage class :
 This is normally not used as it is always implicitly
associated with all local variables. The keyword used for
this storage class is auto. An auto variable is automatically
created and initialized when control enters into a block
and removed when control comes out of block.
 auto int i=30; /*an auto variable with local scope and
extent*/
 Storage Location: Main Memory
 Default Value: Garbage
 Scope: Local to the block in which variable is declared
Lifetime: till the control remains within the block.
 Example: Program to illustrate how auto variables work
 main()
{
 auto int i=10;
 clrscr();
{
 auto int i=20;
 printf("%d",i);
}
 printf("\t%d",i);
}
 o/p:
 20
 10
register or Register Storage class
 We can tell the compiler that a variable should be kept in registers (which are
memory locations on microprocessors used for internal operations), instead of
keeping in the memory (where normal variables are stored).

Since a register access is much faster than a memory access, keeping the
frequently accessed variables (e.g. loop control variables) in the registers will
lead to faster execution of program. Most of the compilers allow only int or char
variables to be placed in the register.
 register int i;

The above is a request to the compiler to allocate a register to variable i instead
of allocating memory. If the register is available then i is placed in register,
otherwise it is as usual given memory location.

The programmer doesn't know whether register is allocated or memory is
allocated to a variable that is used with register storage class. So, it is not
possible to use address operator (&) with variables that contain register storage
specifier. This is of the fact that registers do not contain address.

It is not applicable for arrays, structures or pointers. It cannot not used with
static or external storage class.

 Storage Location: CPU registers
 Default Value: Garbage
 Scope: Local to the block in which variable is declared
Lifetime: till the control remains within the block.
 Since, there are limited number of register in processor
and if it couldn't store the variable in register, it will
automatically store it in memory
 main()
{
 register int i;
 for(i=0;i<10000;i++) . . . . }
 Static or Static Storage Class
 static is the default storage class for global variables. This is
used to promote local extent of a local variable to static extent.

 The value of static variables persists until the end of the
program. A variable can be declared static using the keyword
static like
 static int x; static float y;

 When you use static storage class while declaring local
variable, instead of creating the variable and initializing it
whenever control enters the block, the variable is created and
initialized only for once - when variable's declaration is
encountered for first time. So a variable with static extent will
remain in the memory across function calls.

 Storage Location: Main Memory
 Default Value: Zero
 Scope: Local to the block in which variable is declared Lifetime: till
the value of the variable persists between different function calls.
 Example: Program to illustrate the properties of a static variable.
 main()
 {
 int i;
 for (i=0; i<3; i++)
 incre();
 }
 incre()
 {
 int avar=1;
 static int svar=1;
 avar++;
 svar++;
 printf("\n Automatic variable value : %d",avar);
}
 Output:
 Automatic variable value: 2 Static variable value: 2
Automatic variable value: 2 Static variable value: 3
Automatic variable value: 2 Static variable value: 4
 extern or External Storage class: extern is used to
give a reference of a global variable that is visible to
all the program files. When you use 'extern' the
variable cannot be initialized as all it does is point
the variable name at a storage location that has been
previously defined.

 External variable can be accessed by any function.
They are also known as global variables. Variables
declared outside every function are external
variables.
/* File1.c */
extern int count; /* refers to an external variable count */ inccount() {
count++; }

/* File2.c */ #include"File1.c" int count; /* creates variable count with
static extent */ main() { . . . }

In this case program File1.c has to access the variable defined in program
File2.c.

Storage Location: Main Memory
Default Value: Zero
 Scope: Global/File Scope
 Lifetime: as long as the program execution doesn't come to an end.

Example: Program to illustrate extern storage class write.c void write_ex
 #include"File1.c" int count; /* creates variable count with
static extent */
 main()
 { ...}

 In this case program File1.c has to access the variable
defined in program File2.c.
 Storage Location: Main Memory
 Default Value: Zero
 Scope: Global/File Scope
 Lifetime: as long as the program execution doesn't come to
an end.

 Example: Program to illustrate extern storage class write.c
 void write_extern();
 extern int count;
 void write_extern()
{
 count = count + 2;
 printf("Count is %d\n",count);
}

 mmain.c #include"write.c“
 int count=5;
 main()
{
 write_extern();
 write_extern();
 variable.h /* program1 */
 extern int num1 = 9;
 extern int num2 = 1;
 extern.c /* program2 */
 #include <stdio.h>
 #include "variable.h"
 int main()
{
 int add = num1 + num2;
 printf("%d + %d = %d ", num1, num2, add);
 return 0;
}
 o/p:
 9 + 1 = 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy