Programming in "C‟ | Describe auto and external storage classes with example.

Automatic variables: These are declared inside a function in which they are to be used. They are created when a function is called and destroyed when the function completes its execution. They are private to the function. Therefore these variables are also known as local or internal variables. To declare automatic variables explicitly the keyword auto can be used. The values of automatic variables defined in a function cannot be changed by some other function. 

Example: 
void main() {
auto int a;
a=10;
printf(“%d”,a);
}

External variables: these variables are active and alive throughout the entire program. These are also known as global variables. These variables can be accessed by any function in the program. External
variables are declared outside a function. In case a local variable and global variable has the same name, the local variable will have preference over the global variable. The value of a global variable can be changed by any function, the subsequent functions will refer to the new value.

Example:
int number;
void main() {
number=10;
printf(“%d”,number);
}
void function1() {
number=20;
printf(“%d”,number);
}\ 

Post a Comment

0 Comments