Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Storage classes.

Similar presentations


Presentation on theme: "C Storage classes."— Presentation transcript:

1 C Storage classes

2 Storage classes in C Storage Classes are used to describe about the features of a variable/function. These features basically include the scope, default initial values and life- time which help us to trace the existence of a particular variable during the runtime of a program. Storage classes also tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable.

3 Storage classes in C Scope: i.e. where the value of the variable would be available inside a program. Default initial value: i.e. if we do not explicitly initialize that variable, what will be its default initial value. Lifetime of that variable: i.e. for how long will that variable exist. Storing values: 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.

4 Storage classes in C Automatic variables External variables
Static variables Register variables The general form of a variable declaration that uses a storage class is shown here: storage_class_specifier data_type variable_name;

5 Storage classes in C (Automatic variables)
Automatic variables can also be called local variables because they are local to a function. By default they are assigned garbage value by the compiler. Storage place: CPU Memory Initial/default value: Garbage value Scope: local Life: Within the function only.

6 Storage classes in C (Automatic 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); #include<stdio.h> void main() { int detail; // or auto int details; //Both are same }

7 Storage classes in C (External variables)
The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program. Means we can declare variable and define at any place. Storage place: CPU memory Initial/default value: Zero Scope: Global Life: Till the end of the main program. Variable definition might be anywhere in the C program.

8 Storage classes in C (External variables)
int main() { extern int x; //informs the compiler that it is defined somewhere else printf("%d", x); } int x=2; //Global variable x Also, if you change the statement extern int x; to extern int x = 50; you will again get an error "Redefinition of 'x'" because with extern specifier the variable cannot be initialized, if it is defined elsewhere.

9 Storage classes in C (Static variables)
A static variable tells the compiler to persist/save the variable until the end of program. Storage place: CPU memory Initial/default value: Zero Scope: local Life: Retains the value of the variable between different function calls.

10 Storage classes in C (Static variables)
void test() { static int a = 0; //a static variable a = a + 1; printf("%d\t",a); } #include<stdio.h> void test(); //Function declaration (discussed in next topic) int main() { test(); }

11 Storage classes in C (register variables)
The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size. Can't have the unary '&' operator applied to it (as it does not have a memory location). Use: A register declaration is equivalent to an auto declaration, but hints that the declared variable will be accessed frequently; therefore they are placed in CPU registers, not in memory. Only a few variables are actually placed into registers

12 Storage classes in C (register variables)
int main() { register int i = 10; // int i=10; printf("Value of i: %d", i); printf("\n Address of i: %u", &i); } Value of i: 10 Address of i: Storage place: Register memory Initial/default value: Garbage value Scope: local Life: Within the function only.

13 Assignment Write sample C program to check variability scope of auto storage class. Write simple C program to find factorial of given range number using static storage class concept 1 fact is 1 2 fact is 2 3 fact is 6 Write any c program to find use of external storage class Write any c program to show drawbacks of register storage class

14

15 #include<stdio.h>
void test(); //Function declaration (discussed in next topic) int main() { int i; for (i=1;i<=5;i++){ test(i); } void test(int aa) static int a = 1; //a static variable a = a *aa; printf("%d fact is %d\n",aa, a);


Download ppt "C Storage classes."

Similar presentations


Ads by Google