There are four storage classes in C programming language, which are as follows −
- auto
- extern
- static
- register
Automatic variables / Local variables
The keyword is auto. These are also called as local variables.
Scope
- Scope of a local variable is available within the block in which they are declared.
- These variables are declared inside a block.
- Default value: Garbage value.
Algorithm
The algorithm is given below −
START Step 1: Declare and initialize auto int i=1 I. Declare and initialized auto int i=2 I. declare and initialized auto int i=3 II. print I value//3 II Print I value //2 Step 2: print I value STOP
Example
Following is the C program for auto storage class −
#include<stdio.h> main ( ){ auto int i=1;{ auto int i=2;{ auto int i=3; printf ("%d",i) } printf("%d", i); } printf("%d", i); }
Output
The output is stated below −
3 2 1
Consider another program for auto storage class.
Example
#include<stdio.h> int mul(int num1, int num2){ auto int result; //declaration of auto variable result = num1*num2; return result; } int main(){ int p,q,r; printf("enter p,q values:"); scanf("%d%d",&p,&q); r = mul(p, q); printf("multiplication is : %d\n", r); return 0; }
Output
The output is stated below −
Run 1: enter p,q values:3 5 multiplication is : 15 Run 2: enter p,q values:6 8 multiplication is : 48