



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Storage class in C decides the part of storage to allocate memory for a variable, it also determines the scope of a variable. All variables defined in a C program get some physical location in memory where variable's value is stored. Memory and CPU registers are types of memory locations where a variable's value can be stored. The storage class of a variable in C determines the life time of the variable if this is 'global' or 'local'.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Storage class in C decides the part of storage to allocate memory for a variable, it also determines the scope of a variable. All variables defined in a C program get some physical location in memory where variable's value is stored. Memory and CPU registers are types of memory locations where a variable's value can be stored. The storage class of a variable in C determines the life time of the variable if this is 'global' or 'local'. Along with the life time of a variable, storage class also determines variable's storage location (memory or registers), the scope (visibility level) of the variable, and the initial value of the variable. There are four storage classes in C those are automatic , register , static , and external.
There are four storage class specifiers in C as follows, typedef specifier does not reserve storage and is called a storage class specifier only for syntactic convenience. It is not a storage class specifier in the common meaning. ■ auto ■ register ■ extern ■ static ■ typedef These specifiers tell the compiler how to store the subsequent variable. The general form of a variable declaration that uses a storage class is shown here: storage_class_specifier data_type variable_name; At most one storage class specifier may be given in a declaration. If no storage class specifier is specified then following rules are used:
within the file in which they are declared. Local variables have no linkage and are therefore known only within their own block.
There are four storage classes in C they are as follows:
A variable defined within a function or block with auto specifier belongs to automatic storage class. All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned. Variables having automatic storage class are local to the block which they are defined in, and get destroyed on exit from the block. The following C program demonstrates the visibility level of auto variables. #include <stdio.h> int main( ) { auto int i = 1; { auto int i = 2; { auto int i = 3; printf ( " \n %d ", i); } printf ( "%d ", i); } printf( "%d \n ", i); } OUTPUT ====== 3 2 1
In above example program you see three definitions for variable i. Here, you may be thinking if there could be more than one variable with the same name. Yes, there could be if these variables are defined in different blocks. So, there will be no error here and the program will compile and execute successfully. The printf in the inner most block will print 3 and the variable i defined in the inner most block gets destroyed as soon as control
exits from the block. Now control comes to the second outer block and prints 2 then comes to the outer block and prints 1. Here, note that automatic variables must always be initialized properly, otherwise you are likely to get unexpected results because automatic variables are not given any initial value by the compiler.
void staticDemo() { static int i; { static int i = 1; printf("%d ", i); i++; } printf("%d \n ", i); i++; }
int main() { staticDemo(); staticDemo(); } OUTPUT ====== 1 0 2 1
■ When static specifier is applied to a global variable or a function then
compiler makes that variable or function known only to the file in which it is defined. A static global variable has internal linkage that means even though the variable is global; routines in other files have no knowledge of it and cannot access and alter its contents directly. The following C program defines one static global variable gInt and a static function staticDemo(), for the variable and function are defined static they cannot be used outside the file (translation unit) staticdemo.c.. /* staticdemo.c */ #include <stdio.h> static int gInt = 1; static void staticDemo() { static int i; printf("%d ", i); i++; printf("%d \n ", gInt); gInt++; }
int main() { staticDemo(); staticDemo(); } OUTPUT ======
0 1 1 2
Static variables have default initial value zero and initialized only once in their lifetime.
The extern specifier gives the declared variable external storage class. The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program. To understand why this is important, it is necessary to understand the difference between a declaration and a definition. A declaration declares the name and type of a variable or function. A definition causes storage to be allocated for the variable or the body of the function to be defined. The same variable or function may have many declarations, but there can be only one definition for that variable or function. When extern specifier is used with a variable declaration then no storage is allocated to that variable and it is assumed that the variable has already been defined elsewhere in the program. When we use extern specifier the variable cannot be initialized because with extern specifier variable is declared, not defined. In the following sample C program if you remove extern int x; you will get
an error "Undeclared identifier 'x'" because variable x^ is defined later than it has been used in printf. In this example, the extern specifier tells the compiler that variable x has already been defined and it is declared here for compiler's information. #include <stdio.h> extern int x; int main() { printf("x: %d \n ", x); } int x = 10;