Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
03
Title of Experiment:
Theory:
Functions:
C functions are basic building blocks in a program. All C programs are written using functions to improve
re-usability, understandability and to keep track on them.
2. USES OF C FUNCTIONS:
C functions are used to avoid rewriting same logic/code again and again in a program.
There is no limit in calling C functions to make use of same functionality wherever required.
We can call functions any number of times in a program and from any place in a program.
The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve
the functionality and to improve understandability of very large C programs.
As you know, functions should be declared and defined before calling in a C program.
1 #include<stdio.h>
6 int main( )
7 {
8 float m, n ;
11 // function call
12 n = square ( m ) ;
14 }
15
17 {
18 float p ;
19 p=x*x;
20 return ( p ) ;
21 }
OUTPUT:
Enter some number for finding square
Storage Classes in C
Storage class specifiers in C language 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.
Syntax:
storage_specifier data_type variable _name;
1. auto
2. extern
3. static
4. register
NOTE:
For faster access of a variable, it is better to go for register specifiers rather than auto specifiers.
Because, register variables are stored in register memory whereas auto variables are stored in
main CPU memory.
Only few variables can be stored in register memory. So, we can use variables as register that
are used very often in a C program.
The scope of this auto variable is within the function only. It is equivalent to local variable. All local
variables are auto variables by default.
1 #include<stdio.h>
2 void increment(void);
3 int main()
4 {
5 increment();
6 increment();
7 increment();
8 increment();
9 return 0;
10 }
11
12 void increment(void)
13 {
14 auto int i = 0 ;
16 i++;
17 }
OUTPUT:
0000
Static variables retain the value of the variable between different function calls.
Program:
#include<stdio.h>
void increment(void);
int main()
increment();
increment();
increment();
increment();
return 0;
void increment(void)
{
static int i = 0 ;
i++;
OUTPUT:
0123
The scope of this extern variable is throughout the main program. It is equivalent to global variable.
Definition for extern variable might be anywhere in the C program.
1 #include<stdio.h>
3 int x = 10 ;
4 int main( )
5 {
6 extern int y;
9 return 0;
10 }
11 int y=50;
OUTPUT:
The value of x is 10
The value of y is 50
Register variables are also local variables, but stored in register memory. Whereas, auto
variables are stored in main CPU memory.
Register variables will be accessed very faster than the normal variables since they are stored in
register memory rather than main memory.
But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits
or 64 bits)
1 #include <stdio.h>
2 int main()
3 {
4 register int i;
7 arr[1] = 20;
8 arr[2] = 30;
9 arr[3] = 40;
10 arr[4] = 50;
11 for (i=0;i<5;i++)
12 {
15 }
16 return 0;
17 }
OUTPUT:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
Conclusion:
In this way ,we had studied and perform program to understand the concept of storage classes and
functions.