Best Answer

Global Varible:

The variable which is declared as "Global" one : having the preveleges to use and access that variable in any class and object( means any where in the program) just like PUBLIC keyword in OOPS concepts.

Static Variable :

If we declare a variable as Static , then it wont have the permission to access that variable through out the program and u have to use it inside the class or object which u declared itself.

All the Best


User Avatar

Wiki User

โˆ™ 2011-09-12 15:38:50
This answer is:
User Avatar
Study guides

What is a programming language

What does DOS stand for

What is a software that is distributed for free

What is application software

See all cards
27 Reviews

Add your answer:

Earn +20 pts
Q: Describe about storage allocation and scope of global extern static local and register variables?
Write your answer...
Still have questions?
magnify glass
Related questions

What is the storage allocation and scope of global extern static local and register variables?

Extern is not a storage allocation specifier, it declares external linkage. That is, the declared variable or constant will be defined in a separate translation unit. Variables declared in global scope are allocated in static memory, as are all constants and static local variables. Local (non-static) variables are allocated on the (current) call stack. In multi-threaded applications, each thread has its own call stack. Register variables are automatic variables allocated within a CPU register, however the language compiler ultimately decides when or indeed if this is appropriate.

Describe about storage allocation?

Storage allocation is a method of saving overall disk space in computer programming terms. This method involves saving similar pieces of data in the same location.

What is register variable in c language?

Register variables are a special case of automatic variables. Automatic variables are allocated storage in the memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing in the CPU. These computers often have small amounts of storage within the CPU itself where data can be stored and accessed quickly. These storage cells are called registers. Normally, the compiler determines what data is to be stored in the registers of the CPU at what times. However, the C language provides the storage class register so that the programmer can ``suggest'' to the compiler that particular automatic variables should be allocated to CPU registers, if possible. Thus, register variables provide a certain control over efficiency of program execution. Variables which are used repeatedly or whose access times are critical, may be declared to be of storage class register. Also these register variables are used in huge projects the tiny program developers are not interested to include these register variables, because the tiny programs never requires more time complete its job. These register variables may be used to store constant values so as to make use of it anywhere in the programs. main{ register float a=0;}

What is difference in static and dynamic storage allocation?

Static storage allocation is when a program dedicates an amount of memory for its use at the start of the program. Dynamic storage allocation is when a program only takes storage as it needs it.

What is auto declaration in C?

Auto is one of the four storage classes in c. This is the default storage class. The auto storage class can be used only inside functions, i.e. only to declare local variables and not to declare global variables. All the local variables are by default auto variables. Other storage classes are: Register - variables declared may get stored in CPU registers instead of RAM Static - default storage class for global variables extern - defines global variables that is visible to all object modules

What is the Difference between auto and register storage classes in c language?

register behaves like auto class but auto variables are actually local variables.They are created when the functions are created but they are destroyed when the fns is exited.But register-variables are used to speed up the operations

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 register storage class?

Register storage class is a compiler hint that the variable will be often used, and that it should generate code, if it can, to keep the variable's value in a register.

What is a register variable in c programming?

A register variable is a variable that we wish to allocate in a CPU register rather than in a memory address (RAM). Register variables must be less than or equal in length to the word length of a CPU register which typically limits their use to integral types and pointers. Typical uses for register variables include counters, accumulators and other regularly-accessed variables within a localised code block. Storage classes were originally inherited from B, however only the static and extern storage classes have any meaningful purpose in C. The register storage class is largely redundant while the auto storage class is wholly redundant. Explicitly declaring a register variable is no guarantee the variable will actually be allocated to a register, it is merely a hint to the compiler that the variable is a candidate. However, modern compilers are perfectly capable of determining which variables make good candidates without hints, so there is typically no need to explicitly declare them. If we have more good candidates than there are registers available, explicitly declaring our preferred candidates might be considered worthwhile, however it's generally better to just let the compiler decide. Note that register variables have no memory address. If we take the address of any variable using the unary '&' operator, then that variable can no longer be considered as a register variable candidate by the compiler. If we explicitly declare a register variable and subsequently attempt to take its address, the compiler will issue a warning, yet another reason to simply let the compiler decide.

What is contiguous storage allocation?

Contiguous means to share an edge or boundary, touching, adjacent, neighbouring and so on. Thus contiguous storage allocation is any allocation that consumes two or more contiguous storage elements. In the case of contiguous memory allocation, this means two or more contiguous memory addresses are allocated. A one-dimensional array is an example of a contiguous memory allocation, where one array element (a data type) is immediately followed by the next.

What is the use of storage classes in C?

Storage classes were originally inherited from the B programming language (the language from which C evolved). There are four storage classes: automatic, external, registerand static. The exact meaning of these storage classes depends on whether they are implicitly or explicitly declared and in what context they are used.All local variables, including formal arguments, have automatic storage by default. All this really means is that they are allocated on the call stack. Given this is done automatically for all local variables and formal arguments, we do not need to specify the automatic storage class; it is implied. The auto keyword serves no other purpose in C so we seldom see it in production code, if at all. Its presence in the language is historical rather than functional.Local variables (but not formal arguments) can be explicitly declared static. Static local variables are allocated in the program's data segment and are accessible to all instances of the function in which they are declared whereas automatic variables are only accessible to the current instance of the function. Being allocated in the data segment also means the variable is accessible outwith the scope of the function in which it is declared. However, this is best avoided in multi-threaded applications as it can result in data races.Local variables and formal arguments can also be explicitly declared with the register storage class. Register variables are allocated in a CPU register rather than on the call stack. This can be useful for counter variables within for loops, accumulators or other variables that are accessed often. Register variables have no memory address so we must not attempt to take the address of a register variable. The variable also has to be small enough to fit in a register. However, the register storage class should only be regarded as a (strong) hint to the compiler rather than an explicit directive. There are a limited number of registers available so we cannot declare every automatic variable as a register variable, however the compiler's optimisers are perfectly capable of deciding which variables should be register variables and which should be automatic.All global variables are static by default. This means is that the variable is allocated in the program's data segment (as with local static variables). Again, this is implied, however the meaning changes when we explicitly declare a global with static storage. When we do this we limit the scope or visibility of that global to the file in which it is declared. It is still global but it only has internal linkage. If static storage is implied, then it has external linkage.When we declare a global variable with external storage we are not actually allocating any storage at all, we are simply declaring that the global variable is (or will be) defined in another translation unit.All functions have global access with external linkage by default but we can limit their scope to the file in which they are declared by declaring them static, as per global variables. We do not use the external storage class with functions.Note that since C++11 (ISO/IEC 14882:2011) the auto keyword has been redeployed such that an object's type can be deduced from its initialiser (automatic type deduction). Automatic storage can also be used in range-based for loops, typically when iterating over a container's objects. Trailing-return-types also make it possible for template functions to deduce return types from incomplete types. These redefinitions will never be migrated to the C programming language without fundamental changes to the language itself.

What are the different storage class in c?

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.

People also asked