How to write multiline macro in C language?

What is multiline macro?

How to write a multiline macro in C programming language. We typically define macros that span over a single line. However, there exist conditions whenever you need to outline a macro that spans over a number of lines.

In this put up I’ll clarify how to write a multiline macro in C language. So allow us to get began.

How To Write Multiline Macro In C Language?

Required data

Basic C programming, Preprocessor directives, Macros

During the course of macros programming workout routines, we learned the fundamentals of macros. How to outline and undefine a macro. In this put up, we are going to continue forward with macro and will be taught to define a multiline macro.

For a lot of the instances, macros are candy and short, prolonged up to a single line. However, typically we outline difficult macros that span over a number of traces. Defining these macros in a single line will lose code readability, therefore we outline multiline macro.

Read extra How to print the supply code of a program?

To define a multiline macro append lash on the finish of every line of a macro.

Program to define a multiline macro in C

#include <stdio.h>

// Macro to check and print even odd number
#define EVEN_ODD(num) \
if (num & 1) \
printf("%d is odd\n", num); \
else \
printf("%d is even\n", num);

int main()
{
int num;

// Input number from user
printf("Enter any number: ");
scanf("%d", &num);

EVEN_ODD(num);

return 0;
}

Note: The last line of the macro should not comprise \ symbol

Enter any quantity: 11 11 is odd

C Programming Recommended Post

Get Salesforce Answers here

Leave a Reply

%d bloggers like this: