Program in C Language with Practical
Categories: C language
Example 1: Consider a program to use the #define pre-processor in C.
Define.c
#include <stdio.h>
#include <conio.h>
#define PI 3.14 // define the constant variable
void main()
{
printf (" Display the PI Constant value using the #define is: %f", PI);
getch();
}
Output:
Display the PI Constant value using the #define is: 3.14
Example 2: Consider a program to get the area of a circle in C using the #define pre-processor.
Area.c
# include <stdio.h>
#include <conio.h>
#define PI 3.14 // define the constant variable
void main()
{
int r, area;
printf (" Enter the radius of the circle: ");
scanf ("%d", &r);
area = PI * r * r;
printf (" The area of the circle is: %d", area);
getch();
}
Output:
Enter the radius of the circle : 5
The area of the circle is: 78
In the above program, we use PI as a continuous variable using the #define pre-processor. And when the program is executed, the PI value automatically calls in a program.
Example 3: Consider a program to demonstrate the importance of typedef over the #define for data types.
Pgrm.c
#include <stdio.h>
#include <conio.h>
typedef char* ptr;
#define PTR char
int main()
{
ptr x, y, z;
PTR p, q, r;
printf (" sizeof x : %d\n", sizeof(x) );
printf (" sizeof y : %d\n", sizeof(y) );
printf (" sizeof z : %d\n", sizeof(z) );
printf (" sizeof p : %d\n", sizeof(p) );
printf (" sizeof q : %d\n", sizeof(q) );
printf (" sizeof r : %d\n", sizeof(r) );
return 0;
}
Output:
Sizeof x:8
Sizeof y:8
Sizeof z:8
Sizeof p:8
Sizeof q:1
Sizeof r:1