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

Storage Classes in C

sample

Uploaded by

ksai.mb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Storage Classes in C

sample

Uploaded by

ksai.mb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Definition of Storage Classes in C

Storage Classes in C are attributes that provide information


about the storage, lifetime, and visibility of variables and
functions within a C program. They help determine the scope,
lifetime, and memory allocation of these identifiers. In C, there
are four primary storage classes: auto, register, static, and
extern.

Auto Storage class


The `auto` storage class is the default storage class for all local
variables declared within a function or a block. These variables
are automatically allocated storage in the memory (RAM) and
their lifetime is limited to the block or function where they are
declared. The key characteristics of the `auto` storage class are:

 Storage: Memory (RAM)


 Lifetime: Within the block/function where it is declared
 Initial Value: Garbage value (uninitialised)
 Scope: Local
It is rare to explicitly use the `auto` keyword, as local variables
are implicitly considered `auto`.

#include<stdio.h>

void func() {
auto int x = 1;
printf("%d\n", x);
x += 1;
}

int main() {
func();
func();
return 0;

1
}
Output
1
1
In this example, the 'x' variable is declared as 'auto'. Each time
the 'function()' is called, the value of 'x' is initialised to 1,
incremented by one, and then it goes out of scope once the
function ends. Calling the function multiple times will not retain
the previous value of 'x'.

Register Storage Class in C: benefits and limitations

The `register` storage class is used with local variables that


require faster access. Variables with this storage class are
stored in the CPU registers rather than in memory (RAM),
which allows for faster processing. However, the number of
registers is limited and may not be available for all variables.
Thus, the compiler may store the `register` variables in
memory if it runs out of registers. Key characteristics of the
`register` storage class are:

 Storage: CPU Registers


 Lifetime: Within the block/function where it is declared
 Initial Value: Garbage value (uninitialised)
 Scope: Local
Additionally, the address operator `&` cannot be applied to
`register` variables, as they do not have a memory address.
Here's an example of using a register variable in a C program:
#include<stdio.h>
void func() {
register int x = 1;
printf("%d\n", x);

2
x += 1;
}

int main() {
func();
func();
return 0;
}

In this example, the register int x variable is suggested

to be stored in the CPU register.

Static Storage Class in C: distinguishing it from other

classes

The `static` storage class has a dual role. First, when used with
local variables, it enables the variables to retain the value
between function calls. These static local variables are initialised
only once, no matter how many times the function is called.
Secondly, when used with global variables or functions, it
restricts their scope to the file they are declared in. The main
properties of `static` storage class are:

Storage: Memory (RAM)

Lifetime: Throughout program execution

Initial Value: Zero (0) for variables; functions remain in memory

Scope: Local (variables) and global (functions)


#include<stdio.h>

3
void func() {
static int x = 1;
printf("%d\n", x);
x += 1;
}

int main() {
func();
func();
return 0;
}
Output
Value of x: 1
Value of x: 2
In this example, the 'x' variable is declared with the 'static'
storage class inside the 'function()'. Although the variable is local
to the function, its value is retained between function calls. As a
result, when we call the 'function()' multiple times, the value of 'x'
is incremented and keeps track of the number of times the
function has been called.

Extern Storage Class in C: use and implications

The `extern` storage class is used to tell the compiler about the
existence of a variable or a function that is defined in another
program (file). The primary aim of using the `extern` storage
class is to access and share these external variables or functions
across different program files. Here are the main features of the
`extern` storage class:

 Storage: Memory (RAM)


 Lifetime: Throughout program execution
 Initial Value: Depends on where the variable or function is
defined
 Scope: Global

4
Using the `extern` keyword with a variable or a function ensures
that its storage is not allocated multiple times. It also prevents
errors and ambiguities that may arise from redeclaring these
variables or functions in different program files.
EXTERNAL STORAGE CLASS
Definition and Example of External Storage Class
External storage class in C, represented by the keyword
'extern', is used to give a reference of a global variable
that is visible to all program files. When a variable is
declared with the 'extern' keyword, it indicates that the
variable is defined in another file and the current file is
using its reference.

#include<stdio.h>
int x; // x is a global variable

void func() {
extern int x; // x is referenced here
printf("%d\n", x);
x += 1;
}

int main() {
x = 1;
func();
func();
return 0;
}
Output:
1
2

5
Ex1.c
#include”other.c”
#include<conio.c>

Other.c

6
7

You might also like