The scope and lifetime of variables in functions:-Variables in C different behavior from those in most other language. For example in a basic program a variable returns its value through out the program. It is not always the case in ‘C’. It always depends on the storage classes the variable may assume.
A variable in C can have any one of the four storage classes.
- Automatic variables
- Static Variables
- External Variables or Global Variables
- Register Variables.
Automatic variables:-
These variables are declared inside the function block.
Storage – Main Memory
Default value – Garbage value
Scope – Local to the block in which it is defined.
Life – The control remains within the block in which it is Defined.
Keyword – auto
Program: #include<stdio.h> #include<conio.h> void main() { auto int a,b; clrscr(); printf("\n a=%d",a); printf("\n b=%d",b); getch(); }
Static Variables:-
The memory of static variables remains unchanged until the end of the program.
Storage – Main Memory
Default value – Zero
Scope – Local to the block in which it is defined.
Life – A Value of static variable consists between different function calls. (No Changes). That means it cannot realized between different function calls.
Keyword – static
Program: #include<stdio.h> #include<conio.h> void main() { static int a,b; clrscr(); printf("\n a=%d",a); printf("\n b=%d",b); getch(); }
External Variables or Global Variables:-
The variables both alive and active throughout entire program is known as external variable.
Storage – Main Memory
Default value – Zero
Scope – global
Life – as long as the program execution does not come to end
Keyword – extern
Program:1 #include<stdio.h> #include<conio.h> int a,b; void main() { clrscr(); printf("\n a=%d",a); printf("\n b=%d",b); getch(); }
Program:2 #include<stdio.h> #include<conio.h> int a=100; void main() { extern int a; clrscr(); printf("\n a=%d",a); getch(); }
Register Variables:-
We can use register variables for frequently used variables the fast execution of a program. These variables are declared inside the function block.
Storage – CPU, Register
Default value – garbage value
Scope – local to the block in which it is defined
Life – the control remains within the block in which it is defined
Keyword – Register
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
register int a,b;
clrscr();
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
getch();
}
Note:- If there is no storage classes specifier before the function declaration of variables inside the function block by default it takes auto storage class.
Recursive function:-
Calling a function with the same name function definition is known as recursive function. If you want to work with recursive function we must follow two conditions.
- Calling itself
- Termination condition
Program: Write a program to display natural numbers from 1 to given number using recursive function
#include<stdio.h>
#include<conio.h>
void main()
{
void disp(int);
int n;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
disp(n);
getch();
}
void disp(int x)
{
if(x>1)
disp(x-1);
printf("%d\t",x);
}
Program: Write a program to calculate and display factorial of given number #include<stdio.h> #include<conio.h> void main() { unsigned long fact(int); int n; unsigned long f; clrscr(); printf("Enter any number:"); scanf("%d",&n); f=fact(n); printf("Factorial is:%lu",f); getch(); } unsigned long fact(int x) { if(x<=1) return 1; else return x*fact(x-1); }