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

Storage Classes

The document discusses different storage classes in C language - automatic, extern, static and register variables. Automatic variables are local to a function and destroyed after function exits. Extern variables are declared elsewhere and can be accessed from other files. Static variables retain value between function calls. Register variables are stored in registers for faster access but cannot be addressed.

Uploaded by

Ajay Verma
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)
25 views

Storage Classes

The document discusses different storage classes in C language - automatic, extern, static and register variables. Automatic variables are local to a function and destroyed after function exits. Extern variables are declared elsewhere and can be accessed from other files. Static variables retain value between function calls. Register variables are stored in registers for faster access but cannot be addressed.

Uploaded by

Ajay Verma
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/ 4

Storage classes

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.

In simpler words, In C language, each variable has a storage class which decides scope, visibility and
lifetime of that variable. The following storage classes are most often used in C programming:

• Automatic variables
• Extern variables
• Static variables
• Register variables

Automatic variables

A variable declared inside a function without any storage class specification, is by default an automatic
variable. They are created when a function is called and are destroyed automatically when the function
exits. Automatic variables can also be called local variables because they are local to a function. By
default they are assigned garbage value by the compiler.

Syntax:

int detail;
or
auto int detail; //Both are same

Example:

void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}

Output :

Num : 60
Num : 20

Prepared by: Madhu Chauhan, Assistant professor (IT), IINTM


Explanation:

In the above example, num=60 will be valid in internal scope (curly brackets) and num=20 is valid in
outer scope. In the internal scope, 60 is overwritten by 20.

exern variable

The extern keyword is used before a variable to inform the compiler that this variable is declared
somewhere else. The extern declaration does not allocate storage for variables.

Example 1:

Explanation:

In the above example, we have defined a variable named ‘a’ in file1.c program whose value is 7. In the
other program i.e. file2.c we are accessing the variable which is defined in some other file/program.
This is because in file 2 we have declared ‘a’ as extern variable and also included file1.c in the header
file.

extern variable are sometimes called as global variables. A variable that is declared outside any function
is a Global variable. Global variables remain available throughout the entire program. One important
thing to remember about global variable is that their values can be changed by any function in the
program.

Example:

main()
{
extern int x; //Tells compiler that it is defined somewhere else
x = 10;
printf("%d",x);
}

int x; //Global variable x

Prepared by: Madhu Chauhan, Assistant professor (IT), IINTM


Static variables

A static variable tells the compiler to persist the variable until the end of program. Instead of creating
and destroying a variable every time when it comes into and goes out of scope, static is initialized only
once and remains into existence till the end of program. A static variable can either be internal or
external depending upon the place of declaration. Scope of internal static variable remains inside the
function in which it is defined. External static variables remain restricted to scope of file in each they are
declared. They are assigned 0 (zero) as default value by the compiler.

Example 1:

void test(); //Function declaration (discussed in next topic)

main()
{
test();
test();
test();
}
void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}

Output :
1 2 3

Explanation:

In the above example, in function test(), we have declared ‘a’ as static which means that this variable
will retain its value in between the function call also. Initially the value of ‘a’ is 0. This is incremented to
1 (because a=a+1). After printing the value, the control go back to main(). When this function is called
again, initialization is not done again. Rather previous value of a is retained which was 1. And then it is
incremented to 2 (In second call) and later to 3 (in third call).

Example 2:

#include<stdio.h>
void increment(void);
int main()
{
increment();
increment();
increment();
increment();
return 0;
}

Prepared by: Madhu Chauhan, Assistant professor (IT), IINTM


void increment(void)
{
static int i = 0 ;
printf ( "%d ", i ) ;
i++;
}
Output:
0123

Register variable

Register variable inform the compiler to store the variable in register instead of memory. Register
variable has faster access than normal variable. Frequently used variables are kept in register. Only few
variables can be placed inside register.
NOTE : We can never get the address of such variables.

Syntax :
register int number;

Example:

#include <stdio.h>
void main()
{
register int i;
int arr[5];// declaring array
arr[0] = 10;// Initializing array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}
}

Summary:

Storage Place Initial Value Scope Life


auto CPU memory Garbage value Local Within the function only.

extern CPU memory Zero Global Till the end of main program.

static CPU memory Zero Local Retains the value of the variable
between different function call.

register Register memory Garbage value Local Within the function only.

Prepared by: Madhu Chauhan, Assistant professor (IT), IINTM

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