In this article, we will discuss how to write a multi-line macro. We can write multi-line macro same like function, but each statement ends with "\". Let us see with example. Below is simple macro, which accepts input number from user, and prints whether entered number is even or odd.
C
#include <stdio.h>
#define MACRO(num, str) {\
printf("%d", num);\
printf(" is");\
printf(" %s number", str);\
printf("\n");\
}
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num & 1)
MACRO(num, "Odd");
else
MACRO(num, "Even");
return 0;
}
At first look, the code looks OK, but when we try to compile this code, it gives compilation error.
[narendra@/media/partition/GFG]$ make macro
cc macro.c -o macro
macro.c: In function ‘main’:
macro.c:19:2: error: ‘else’ without a previous ‘if’
make: *** [macro] Error 1
[narendra@/media/partition/GFG]$
Let us see what mistake we did while writing macro. We have enclosed macro in curly braces. According to C-language rule, each C-statement should end with semicolon. That's why we have ended MACRO with semicolon. Here is a mistake. Let us see how compile expands this macro.
if (num & 1)
{
-------------------------
---- Macro expansion ----
-------------------------
}; /* Semicolon at the end of MACRO, and here is ERROR */
else
{
-------------------------
---- Macro expansion ----
-------------------------
};
We have ended macro with semicolon. When compiler expands macro, it puts semicolon after "if" statement. Because of semicolon between "if and else statement" compiler gives compilation error. Above program will work fine, if we ignore "else" part.
To overcome this limitation, we can enclose our macro in "do-while(0)" statement. Our modified macro will look like this.
C
#include <stdio.h>
#define MACRO(num, str) do {\
printf("%d", num);\
printf(" is");\
printf(" %s number", str);\
printf("\n");\
} while(0)
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num & 1)
MACRO(num, "Odd");
else
MACRO(num, "Even");
return 0;
}
Compile and run above code, now this code will work fine.
[narendra@/media/partition/GFG]$ make macro
cc macro.c -o macro
[narendra@/media/partition/GFG]$ ./macro
Enter a number: 9
9 is Odd number
[narendra@/media/partition/GFG]$ ./macro
Enter a number: 10
10 is Even number
[narendra@/media/partition/GFG]$
We have enclosed macro in "do - while(0)" loop and at the end of while, we have put condition as "while(0)", that's why this loop will execute only one time.
Similarly, instead of "do - while(0)" loop we can enclose multi-line macro in parenthesis. We can achieve the same result by using this trick. Let us see example.
C
#include <stdio.h>
#define MACRO(num, str) ({\
printf("%d", num);\
printf(" is");\
printf(" %s number", str);\
printf("\n");\
})
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num & 1)
MACRO(num, "Odd");
else
MACRO(num, "Even");
return 0;
}
[narendra@/media/partition/GFG]$ make macro
cc macro.c -o macro
[narendra@/media/partition/GFG]$ ./macro
Enter a number: 10
10 is Even number
[narendra@/media/partition/GFG]$ ./macro
Enter a number: 15
15 is Odd number
[narendra@/media/partition/GFG]$
This article is compiled by
Narendra Kangralkar.
Similar Reads
C Preprocessors Preprocessors are programs that process the source code before the actual compilation begins. They are not part of the compilation process but operate separately, allowing programmers to modify the code before compilation. It is the first step that the C source code goes through when being converted
8 min read
C Preprocessor Directives In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.List of Preprocessor DirectivesThe foll
6 min read
How a Preprocessor works in C? Compiling a C program - Behind the Scene A Preprocessor is a system software (a computer program that is designed to run on computer's hardware and application programs). It performs preprocessing of the High Level Language(HLL). Preprocessing is the first step of the language processing system. Lan
3 min read
Header Files in C In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor.C language uses header files to provide the standard
5 min read
Whatâs difference between header files "stdio.h" and "stdlib.h" ? In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used
4 min read
How to write your own header file in C? As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar
4 min read
Macros and its types in C In C programming, a macro is a symbolic name or constant that represents a value, expression, or code snippet. They are defined using the #define directive, and when encountered, the preprocessor substitutes it with its defined content.ExampleC#include <stdio.h> // Macro definition #define LIM
4 min read
Interesting Facts about Macros and Preprocessors in C In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the â#â symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before
5 min read
# and ## Operators in C In C, # and ## operators are preprocessor operators using in macros for token manipulation. They are known as stringizing and token pasting operators and are used in macro definition with #define preprocessor. In this article, we will learn about these operators and how to use them in C programs.Str
3 min read
How to print a variable name in C? Printing a variable name means printing the identifier that is assigned to the variable. To print it, it should be in the form of string. This can be done by using stringification.Stringification in C is the method to convert the argument of a function like macro to a string. This can be done with t
1 min read