answersLogoWhite

0


Best Answer

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.

Automatic Variables (auto)

All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.

Static Variables (static)

All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.

It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.

External Variables (extern)

External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.

This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.

Register Variables (register)

A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.

Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

== ==

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the different storage class in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Explain the different access storage specifiers available in c?

The storage class specifiers in C and C++ are:autoexternmutableregisterstatictypedefA storage class specifier is used to refine the declaration of a variable, a function, and parameters


What is different storage class in c?

Different from what? Storage classes are auto, register, static, extern and typedef (formally).


What are the different storage class in C programming?

Automatic, register, external, static


What is the by default storage class of any variable in c?

automatic storage class


Storage classes available in c language?

There are four types of storage class or variable in c. 1) auto storage class. 2) register storage class. 3) static storage class. 4) external storage class.


What is the advantages of storage class in c?

advantage of storage classes


Auto in turbo c?

Storage class specifier.


Why you need to declare auto storage class specifier in c language?

We don't. The auto storage class is the default storage class for all local variables and is therefore completely redundant in C. It exists for no other reason than that C evolved from B and inherited all its storage classes (auto, static, extern and register).


What is storage classes in c plus plus?

AUTO EXTERN STATIC are the storage classes in c++


What are the storage classes in c?

A storage class defines the visibility and lifetime of variables or/and functions within a C Program. There are following storage classes which can be used in a C Program: auto register static extern


What is the utility of storage class in c?

In C there are four storage classes: auto, static, extern and register. These storage classes essentially define the scope or visibility of a name (a function or variable). All four are inherited from B, the language from which C evolved.The auto storage class is used to explicitly declare a non-static local variable. However, given that all non-static local variables are implicitly automatic in C, explicit use of the auto storage class is therefore redundant in C. Moreover, in C++11, explicit use of the auto storage class was dropped entirely; the auto keyword is now used for automatic type deduction in C++.The static storage class is used to explicitly declare a static local variable. In addition, all global variables and functions are implicitly static and have external linkage, but if explicitly declared static they have internal linkage only.The extern storage class is used to allow access to a name that has external linkage.The register storage class is used to define a variable that should be allocated in a CPU register rather than in working memory (RAM).


What is storage claases in c language?

storage classes is important part of c language.WHENEVER we define any function in c program then we can call that in every definend class. there are four types of storage class. these are... 1 AUTO OR AUTOMATIC STORAGE CLASS 2 REGISTER STORAGE CLASS 3 STATIC STORAGE CLASS 4 EXTERNALSTORAGE CLASS 1) The features of "AUTOMETIC" storage class are as under some conditions: storage : storage will be in memory. value : garbage value. scope : scope is local to block to the variable. life : till, controls remain within the block. 2) The featurs of "STATIC" storage class are as under some conditions: storage : memory. value : zero. scope : local to block. life : Till control remains within the block. 3) The featurs of "REGISTER" storage class are as under some conditions: storage : register. value : garbage value. scope : local to the block. life : value persists between variable. 4) The feature of "EXTERNAL" storage class are as under some conditions: storage : memory. value : zero. scope : local to block. life : till controls remains within the block.