Vous êtes sur la page 1sur 19

MODULE 2:STORAGE CLASSS

Storage

class Automatic variables Static Variables Extern variables Register variables Storage Class Comparison Macro Macro as a Function Macro VS Function

Variables in C can have not only data type but also storage class. Storage class provides information about the locations of variables and their visibility. It is Storage class which decides the portion of the program within which variables are recognized. C language provides a number of storage class specifiers that can be used to declare scope and lifetime of a variable. The Storage Class Specifiers are of four types as shown below:
Automatic Variables Static Variables External Variables Register Variables

Automatic variables
Declared at the start of the block Memory is allocated automatically upon entry to a block and freed automatically upon exit from the block. The scope of automatic variables is local to the block in which they are declared.

Variables declared inside a function without storage class specification is, by default, an automatic variable.
Are declared using a keyword auto. eg. auto int number; If automatic variables are not initialized they will contain garbage.

Example:int main() { auto int m=1000; function2(); printf(m in main=%d\n,m); } function1() { auto int m = 10; printf(m in function1=%d\n,m); } function2() { auto int m = 100; function1(); printf(m in function2=%d\n,m); }

OUTPUT:m in funtion1=10 m in funtion2=100 m in main=1000

Static Variables
The value of static variables persists until the end of the program. It is declared using the keyword static like static int x; static float y; Automatically initialized to zero upon memory allocation. Static variables are initialized only once, when the program is compiled.

Example:int main() { Check(); Check(); Check(); } void Check() { static int c=0; printf(c=%d\t",c); c+=5; }

OUTPUT:c=0 c=5 c=10

During first function call, it will display 0. Then, during second function call, variable c will not be initialized to 0 again, as it is static variable. So, 5 is displayed in second function call and 10 in third call.

External Variables
These variables are declared outside any function. These variables are active and alive throughout the entire program

Also known as global variables and default value is zero


It is declared using the keyword extern

Example:int a=5; /*a is global variable because it is outside every function*/ int main()

{
} {

a+=4; check();

void check() ++a; /* Variable a is not declared in this function but,works in any function as they are global */ printf("a=%d\n",a);

OUTPUT:a=10

Register Variables
These variables are stored in one of the machines register and are declared using register keyword. eg. register int count; Since register access are much faster than a memory access keeping frequently accessed variables in the register lead to faster execution of program. Dont try to declare a global variable as register. Because the register will be occupied during the lifetime of the program.

Example:int main() { register int i; for(i=10; i>=0; i=i-2) printf(i=%d ",i); return 0;

OUTPUT:i=10 i=8 i=6 i=4 i=2 i=0

STORAGE CLASS COMPARISON


FEATURES KEYWORD INITIAL VALUE STORAGE SCOPE AUTOMATIC STATIC Auto Garbage Memory Local to block Within the block Static Zero Memory Local to block REGISTER Register Garbage Register EXTERNAL extern Zero Memory

Global, till Local to block the program ends Within the block Throughout the program

LIFE TIME

Retains value between different function calls

MACRO
Sometimes while programming, we stumble upon a condition where we want to use a value or a small piece of code many times in a code. Also there is a possibility that the in future, the piece of code or value would change. Then changing the value all over the code does not make any sense. There has to be a way out through which one can make the change at one place and it would get reflected at all the places. This is where the concept of a macro fits in. A Macro is typically an abbreviated name given to a piece of code or a value. Macros can also be defined without any value or piece of code but in that case they are used only for testing purpose.

#define name text It defines a macro with the given name, having as its value the given replacement text. After that wherver the preprocessor sees that name, it will replace it with the replacement text. The most common use for macros is to propagate various constants around and to make them more self documenting.

Example:
#include <stdio.h> #define PI 3.1415 int main() { int radius; float area; printf("Enter the radius: "); scanf("%d",&radius); area=PI*radius*radius; printf(\nArea=%.2f",area); return 0; }

OUTPUT:Enter the radius: 3 Area=28.27

MACRO AS A FUNCTION
#define minimum(a,b) (a < b ? a : b) Void main() { int a = 10, b = 20; printf(The minimum is: ,minimum(a,b)); getch(); } OUTPUT: The minimum is: 10

Macros
Macros are faster than function but taking more memory space . Macros placed in the source code before compilation. If we use a macro hundred times in a program, the macro expansion goes into our source code at hundred different place thus increase program size.

Function
Function are slower tan macros but taking less memory space Passing arguments to a function & getting back the returned value does take time. If a function is called from hundred different places in the program, it would take the same amount of space in the program.

THANK YOU

Vous aimerez peut-être aussi