0% found this document useful (0 votes)
19 views9 pages

Storage Class

The document explains the four storage classes in C programming: auto, register, static, and extern, detailing their scope, lifetime, and default values. It also covers the bitwise complement operator and the left and right shift operators, providing syntax and examples for each. Additionally, it includes sample code snippets demonstrating the behavior of these storage classes and operators.

Uploaded by

shivgovindvns
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)
19 views9 pages

Storage Class

The document explains the four storage classes in C programming: auto, register, static, and extern, detailing their scope, lifetime, and default values. It also covers the bitwise complement operator and the left and right shift operators, providing syntax and examples for each. Additionally, it includes sample code snippets demonstrating the behavior of these storage classes and operators.

Uploaded by

shivgovindvns
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/ 9

STORAGE CLASS: A storage class defines the scope (visibility) and life-time of variables and/or functions within a C

Program. They precede the type that they modify. We have four different storage classes in a C program −

 auto
 register
 static
 extern

Storage Storage Default Scope Lifetime


Classes Place Value
auto RAM Garbage Local Within function
Value
extern RAM Zero Global Till the end of the main program Maybe declared anywhere in
the program
static RAM Zero Local Till the end of the main program, Retains value between
multiple functions call
register Register Garbage Local Within the function
Value
Automatic

 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.
 The keyword used for defining automatic variables is auto.
 Every local variable is automatic in C by default.

Example 1

#include <stdio.h>

#include <conio.h>

void main()

int a; //auto

char b;

float c;

printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.

getch()

Output: garbage garbage garbage


Example 2

#include <stdio.h>

#include <conio.h>

void main()

int a = 10,i;

printf("%d ",++a);

int a = 20;

for (i=0;i<3;i++)

printf("%d ",a);

printf("%d ",a);

getch();

Output: 11 20 20 20 11

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.

Example 1

#include<stdio.h>

#include<conio.h>

static char c;

static int i;

static float f;

static char s[100];

void main ()

{
printf("%d\t%d\t%f %s",c,i,f); // the initial default value of c, i, and f will be printed.

getch();

Output: 0 0 0.000000 (null)

Example 2

#include<stdio.h>

#include<conio.h>

void sum()

static int a = 10;

static int b = 24;

printf("%d %d \n",a,b);

a++;

b++;

void main()

int i;

for(i = 0; i< 3; i++)

sum(); // The static variables holds their value between multiple function calls.

getch();

Output:

10 24

11 25

12 26

Register

 The variables defined as the register is allocated the memory into the CPU registers depending upon the size of
the memory remaining in the CPU.
 We can not dereference the register variables, i.e., we can not use &operator for the register variable.
 The access time of the register variables is faster than the automatic variables.
 The initial default value of the register local variables is 0.
 The register keyword is used for the variable which should be stored in the CPU register. However, it is
compiler?s choice whether or not; the variables can be stored in the register.
 We can store pointers into the register, i.e., a register can store the address of a variable.
 Static variables can not be stored into the register since we can not use more than one storage specifier for the
same variable.

Example 1

#include <stdio.h>

#include <conio.h>

void main()

register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.

printf("%d",a);

getch();

Output: 0

Example 2

#include <stdio.h>

#include <conio.h>

void main()

register int a = 0;

printf("%u",&a); // This will give a compile time error since we can not access the address of a register variable.

getch();

Output: main.c:5:5: error: address of register variable ?a? requested

printf("%u",&a);

^~~~~~

External

 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.

Example 1

#include <stdio.h>

#include <conio.h>

void main()

extern int a;

printf("%d",a);

getch();

Output: main.c:(.text+0x6): undefined reference to `a'

collect2: error: ld returned 1 exit status

Example 2

#include <stdio.h>

#include <sconio.h>

int a;

void main()

extern int a; // variable a is defined globally, the memory will not be allocated to a

printf("%d",a);

getch();

Output: 0

Example 3

#include <stdio.h>

#include <conio.h>

int a;

void main()

extern int a = 0; // this will show a compiler error since we can not use extern and initializer at same time

printf("%d",a);
getch();

Output: compile time error

main.c: In function ?main?:

main.c:5:16: error: ?a? has both ?extern? and initializer

extern int a = 0;

Example 4

#include <stdio.h>

#include <conio.h>

void main()

extern int a; // Compiler will search here for a variable a defined and initialized somewhere in the pogram or not.

printf("%d",a);

getch();

int a = 20;

Output: 20

Example 5

extern int a;

int a = 10;

#include <stdio.h>

#include <conio.h>

void main()

printf("%d",a);

int a = 20; // compiler will show an error at this line

Output: compile time error

Bitwise complement operator

The bitwise complement operator is also known as one's complement operator. It is represented by the symbol tilde (~).
It takes only one operand or variable and performs complement operation on an operand. When we apply the
complement operation on any bits, then 0 becomes 1 and 1 becomes 0.
For example,

If we have a variable named 'a',

a = 8;

The binary representation of the above variable is given below:

a = 1000

When we apply the bitwise complement operator to the operand, then the output would be:

Result = 0111

As we can observe from the above result that if the bit is 1, then it gets changed to 0 else 1.

Let's understand the complement operator through a program.

#include <stdio.h>

#include <conio.h>

void main()

int a=8; // variable declarations

printf("The output of the Bitwise complement operator ~a is %d",~a);

getch();

Output: The output of the Bitwise complement operator ~a is -9

Bitwise shift operators : Two types of bitwise shift operators exist in C programming. The bitwise shift operators will
shift the bits either on the left-side or right-side. Therefore, we can say that the bitwise shift operator is divided into two
categories:

 Left-shift operator
 Right-shift operator

Left-shift operator

It is an operator that shifts the number of bits to the left-side.

Syntax of the left-shift operator is given: Operand << n

Where, Operand is an integer expression on which we apply the left-shift operation. n is the number of bits to be
shifted.

In the case of Left-shift operator, 'n' bits will be shifted on the left-side. The 'n' bits on the left side will be popped out,
and 'n' bits on the right-side are filled with 0.

For example,

int a = 5;
The binary representation of 'a' is given below:

a = 0101

If we want to left-shift the above representation by 2, then the statement would be:

a << 2;

0101<<2 = 00010100

Program.

#include <stdio.h>

#include <conio.h>

void main()

int a=5; // variable initialization

printf("The value of a<<2 is : %d ", a<<2);

getch();

Output: The value of a<<2 is : 20

Right-shift operator: It is an operator that shifts the number of bits to the right side.

Syntax of the right-shift operator is given: Operand >> n;

Where, Operand is an integer expression on which we apply the right-shift operation. N is the number of bits to be
shifted.

In the case of the right-shift operator, 'n' bits will be shifted on the right-side. The 'n' bits on the right-side will be
popped out, and 'n' bits on the left-side are filled with 0.

For example,

Suppose we have a statement,

int a = 7;

The binary representation of the above variable would be:

a = 0111

If we want to right-shift the above representation by 2, then the statement would be:

a>>2;

0000 0111 >> 2 = 0000 0001

Program:

#include <stdio.h>

#include <conio.h>

void main()
{

int a=7; // variable initialization

printf("The value of a>>2 is : %d ", a>>2);

getch();

Output: The value of a>>2 is : 1

You might also like