Table of Contents
hide
Macro Examples
Example : A Preprocessor Directives and Macro Examples in C to display the message/value using Macro.
#include <stdio.h>
#define STR "Welcome U All in Codershelpline"
main()
{
printf(STR);
}
Output :
Welcome U All in Codershelpline
-------------- OR ---------------
#include <stdio.h>
#define PI 3.141
void main()
{
printf("%f",PI);
getch();
}
Output :
3.141000
Example : A Preprocessor Directives and Macro Examples in C to find the square value of a given number using Macro.
#include <stdio.h>
# define SQR(y) (y*y)
main()
{
int num1,result;
printf("Enter a number to find its square value : ");
scanf("%d", &num1);
result = SQR(num1);
printf("\nThe square of %d is %d", num1, result);
}
Output :
Enter a number to find its square value : 12
The square of 12 is 144
Example : A Preprocessor Directives and Macro Examples in C to find the maximum value of given two numbers using Macro.
#include <stdio.h>
#define MAX(p,q) ((p < q) ? (q):(p))
main()
{
int num1,num2,result;
printf("Enter two number to find maximum value : ");
scanf("%d%d", &num1,&num2);
result = MAX(num1,num2);
printf("\nThe max value from %d and %d is = %d", num1,num2,result);
}
Output :
Enter two number to find maximum value : 12
32
The max value from 12 and 32 is = 32
Example : A Preprocessor Directives and Macro Examples in C to find the maximum value of given three numbers using macro.
#include <stdio.h>
#define MAX(p,q) ((p < q) ? (q):(p))
#define LARGEST(p,q,r) ( MAX(p, q) < r) ? (r): (MAX(p, q))
main()
{
int num1,num2,num3,result;
printf("Enter three number to find largest value : ");
scanf("%d%d%d", &num1,&num2,&num3);
result = LARGEST(num1,num2,num3);
printf("\nThe max value among %d %d and %d is %d", num1,num2,num3,result);
}
Output :
Enter three number to find largest value : 50
20
10
The max value among 50 20 and 10 is 50
Example : A C program to find the area of the square & cube of a given number using Macro.
#include <stdio.h>
# define AREASQUARE(y) (y*y)
# define CUBE(x) (x*x*x)
main()
{
int num1,result1,result2;
printf("Enter a number to find its cube and area of square value : ");
scanf("%d", &num1);
result1 = AREASQUARE(num1);
result2 = CUBE(num1);
printf("\nThe square of %d is %d", num1, result1);
printf("\nThe square of %d is %d", num1, result2);
}
Output :
Enter a number to find its cube and area of square value : 10
The square of 10 is 100
The square of 10 is 1000
Example : A C program to use macro if-endif statement through an example.
#include <stdio.h>
#include <conio.h>
#define NUM 15
void main()
{
#if (NUM<10)
printf("The Number is smaller than 10 and is: %d",NUM);
#endif
#if (NUM==10)
printf("The Number is equal to : %d",NUM);
#endif
#if (NUM>10)
printf("The Number is greater than 10 and is : %d",NUM);
#endif
getch();
}
Output :
The Number is greater than 10 and is : 15
Example : A C program to use macro if-else-endif statement through an example.
#include <stdio.h>
#include <conio.h>
#define NUM 900
void main()
{
#if (NUM<=700)
printf("The Number is smaller or equal than given value and is: %d",NUM);
#else
printf("The Number is greater than given value and is : %d",NUM);
#endif
getch();
}
0 Comments