1
# define PI 3.14
Some of the common uses of C preprocessor are:
Including Header Files: #include
#include <stdio.h>
Here stdio.h is a header file. The #include pre-processor directive replaces the above line with the content of stdio.h header file.
Thats the reason you need to include header file before to use the function like scanf() and printf().
2
Macros using #define
A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive.
# define c 29979
Here, when we use c in our program it will be replaced by 29979.
Function like Macros
You can also define macros that work in a similar way like a function call. This is known as function-like macros. For example,
#define circleArea(r) (3.1415*(r)*(r))
Every time the program encounters circleArea(argument), it is replaced by
(3.1415*(argument)*(argument)).
Suppose, we passed 5 as an argument then, it expands as below:
circleArea(5) expands to (3.1415*5*5)
3
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main() {
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);
return 0;
}
Example:
4
5
6
#include <stdio.h>
int main()
{
printf("Current time: %s",__TIME__);
printf("\nFile is %s",__FILE__);
printf("\nFile is %s",__DATE__);
printf("\nFile is %d",__STDC__);
printf("\n File is %d",__LINE__);
}
Current time: 23:50:03
File is /usr/share/IDE_PROGRAMS/C/other/698a5a2b68da57910db370a5d8d8f6b6/698a5a2b68da57910db370a5d8d8f6b6.c
File is Mar 2 2021
File is 1
File is 8
Storage Type in C:
7
The auto Storage Class
8
9
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);}
Output:
3 2 1
Register Storage Class�
10
11
#include <stdio.h> /* function declaration */
main() {
{
register int weight;
int *ptr=&weight ;
/*it produces an error when the compilation occurs ,we cannot get a memory location when dealing with CPU register*/}
}
Static Storage Class in C
12
13
#include <stdio.h>
void next();
static int counter = 7; /* global variable */
int main()
{
while(counter<10)
{
next();
counter++;
}
return 0;
}
void next( )
{ /* function definition */
static int iteration = 13; /* local static variable */
iteration ++;
printf("iteration=%d and counter= %d\n", iteration, counter);
}
Output:
iteration=14 and counter= 7
iteration=15 and counter= 8
iteration=16 and counter= 9
Extern Storage Class in C
14
15
Main.c
#include <stdio.h>
extern i;
main() {
printf("value of the external integer is = %d\n", i);
return 0;}
Orig.c
#include <stdio.h>
i=48;
16
int fun1(int n) {
static int i= 0;
if (n > 0) {
++i;
fun1(n-1);
}
return (i);
}
int fun2(int n) {
static int i= 0;
if (n>0) {
i = i+ fun1 (n) ;
fun2(n-1) ;
}
return (i);
}
The return value of fun2(5) is
Output: 55
17