C++ Programming

Storage Class In C / C++ And How To Use Automatic, Register, Static And External

Storage class:

When a storage class in c/c++ is not present in the declaration the compiler assumes default storage class based on the place of declaration. The storage class besides the following various aspect of variables:

Lifetime: the time between the creation and destruction of a variable.

Scope: location where the variable is available for use

allpcb circuit

Initial value: default value taken by an initial variables

Place of storage: place in memory where the storage is allocated for variables

The general syntax of storage class is :

Class data type  variablename;


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Need of storage class in C/C++:

By the purpose use of storage class in C/C++, the class makes our program efficient and first.

Types of storage class in C/C++:

  • Automatic (auto)
  • External (Extern)
  • Static (static)
  • Register (register)

Automatic Storage Class in C/C++:

In computer programming, an automatic variable is a local variable that is allocated and deallocated automatically when program flow enters and leaves the variable’s scope. The scope is the lexical context, particularly the function or block in which a variable is defined. Local data is typically (in most languages) invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to a called function but is not deallocated, coming back in scope as the execution thread returns to the caller.

All the variables inside block or function without any storage class specifier is called automatic variables and the keyword ‘auto’ is used for automatic storage class.

For example:

function()

{

int a,b;

}

function()

{

auto int a,b;

}

This is called automatic because the storage is reserve automatically each time when the control enter the function or block and the released automatically when the function  or block is terminated.

Example write a program which illustrates the default initial value of an automatic Storage Class in C/C++:

In this program, the variable sum is an automatic storage class. Here the variable sum is not initialized and still, we want to print the value of the sum so compiler it will print and unpredictable value. the value is known as garbage value.



Register Storage Class in C/C++:

The feature of the register storage class is given below

Storage area: CPU register

Default initial value: Garbage value

Scope: Local to the block in which the variable is declared

Write a program which illustrates register storage class in C/C++:

static storage class in C/C++:

In the static storage class, the keyword static is used to declare a static variable. The static variable is not reinitialized but the auto variable is reinitialized. The small difference of auto variable is that the initial value of the auto is garbage value but the static variable initial value is zero.

Static global variable

A variable declared as static at the top level of a source file (outside any function definitions) is only visible throughout that file (“file scope”, also known as “internal linkage”). In this usage, the keyword static is known as an “access specifier”.

Static function

Similarly, a static function – a function declared as static at the top level of a source file (outside any class definitions) – is only visible throughout that file (“file scope”, also known as “internal linkage”).


Write a program which illustrates the work of static storage class in C/C++:

Example how to Write a counter program which illustrates the work of static storage class in C/C++


External storage class in C/C++:

To understand how external variables relate to the extern keyword, it is necessary to know the difference between defining and declaring a variable. When a variable is defined, the compiler allocates memory for that variable and possibly also initializes its contents to some value. When a variable is declared, the compiler requires that the variable be defined elsewhere. The declaration informs the compiler that a variable by that name and type exists, but the compiler does not need to allocate memory for it since it is allocated elsewhere. The extern keyword means “declare without defining”. In other words, it is a way to explicitly declare a variable or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable. If neither the extern keyword nor an initialization value is present, the statement can be either a declaration or a definition. It is up to the compiler to analyze the modules of the program and decide.

The variables that are active and alive throughout the entire program. The external variable is also known as global variables. Here the keyword extern is used to declare an external variable. The external variable is used to outside in function


Write a program which illustrates the work of External storage class in C/C++:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button