Macro Examples

Example : A Preprocessor Directives with Macro Examples for Defining Constants in C to display the message/value.
#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

--------------  OR  ---------------

#include <stdio.h>

#define PI 3.14159  // Defining a constant for PI

int main() 
{
    float radius, area;

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    area = PI * radius * radius;  // Using the PI macro
    printf("The area of the circle is: %.2f\n", area);

    return 0;
}

Output:
Enter the radius of the circle: 4
The area of the circle is: 50.27

--------------  OR  ---------------

#include <stdio.h>

#define SIZE 5  // Define the size of the array

int main() 
{
    int arr[SIZE];  // Array of size 5
    int i;

    printf("Enter %d elements: ", SIZE);
    for (i = 0; i < SIZE; i++) 
	{
          scanf("%d", &arr[i]);
        }

    printf("The elements are: ");
    for (i = 0; i < SIZE; i++) 
	{
          printf("%d ", arr[i]);
        }
    return 0;
}

Output:
Enter 5 elements: 10
20
30
40
50
The elements are: 10 20 30 40 50
Example : A Preprocessor Directives with Macro Examples for Defining a Function like a Macro in C to find the square value of a given number.
#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 with Macro Examples for Defining a Function like a Macro in C to find the maximum value of two numbers.
#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 with Macro Examples for Defining a Function like a Macro in C to find the largest value from three numbers using conditional operators.
#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 by Defining a Function like a 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 perform Swapping of two numbers by Defining a Function like a Macro.
#include <stdio.h>

#define SWAP(a, b)  \
    {               \
        int temp;   \
        temp = a;   \
        a = b;      \
        b = temp;   \
    }

int main() 
{
    int x = 5, y = 10;
    printf("Before swapping: x = %d, y = %d\n", x, y);    
    SWAP(x, y);  // Using the SWAP macro
    printf("After swapping: x = %d, y = %d\n", x, y);
    return 0;
}

Output:
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5

NB: Here,the backslashes(\) symbol allow the macro to span multiple lines.
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();  
}
Example : A C program to use macro #if-#elif-#else-#endif statement through an example.
#include <stdio.h>    
#include <conio.h> 
   
#define NUM 550 
 
void main() 
{ 
	#if (NUM>=700)  
		printf("The Number greater than 700 and is: %d",NUM);	
	#elif (NUM>=600 && NUM<=699)
		printf("The Number is in between 600-699 and is : %d",NUM);		  
	#elif (NUM>=500 && NUM<=599)
		printf("The Number is in between 500-599 and is : %d",NUM);		
	#elif (NUM>=400 && NUM<=499)
		printf("The Number is in between 400-499 and is : %d",NUM);	
	#else
		printf("The Number is below 400 and is : %d",NUM);		
	#endif	
	getch();  
}

Output:
The Number is in between 500-599 and is : 550
Example : A C program example to use a Macro to define #ifdef directives.
#include <stdio.h>

#define ALLOW 

int main() 
{
    #ifdef ALLOW
    	printf("Executed/compiled only when ALLOW is defined, otherwise not");  
    #endif
    return 0;
}

Output:
  Executed/compiled only when ALLOW is defined, otherwise not
Example : A C program to use Macro for Error Handling.
#include <stdio.h>
#include <stdlib.h>

#define CHECK_ERROR(condition, message)         \
    if (!(condition)) 				\
    {                 				\
        printf("Error Message: %s\n", message); \
        exit(EXIT_FAILURE);                     \
    }

int main() 
{
    int x = 0;

    CHECK_ERROR(x != 0, "x must not be zero.");  // Check if x is zero produces CHECK_ERROR message, else print a successful message

    printf("x is not zero and hence program running successfully.\n");
    return 0;
}
Output:
if int x= 0 then generate : Error Message: x must not be zero.
if int x= 10 then generate : x is not zero and hence program running successfully.

NB: 
The CHECK_ERROR macro checks a condition, and if it evaluates to false, it prints an error message and exits the program.
This approach simplifies error handling in a larger program.

Loading

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.