Multiline Mcros In C https://www.skillvertex.com/blog Tue, 05 Mar 2024 12:55:30 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Multiline Mcros In C https://www.skillvertex.com/blog 32 32 Multiline Macros In C https://www.skillvertex.com/blog/multiline-macros-in-c/ https://www.skillvertex.com/blog/multiline-macros-in-c/#respond Tue, 05 Mar 2024 12:55:30 +0000 https://www.skillvertex.com/blog/?p=3318 Read more]]>

Table of Contents

Multiline Macros In C

A multi-line macro allows you to create a more complex and reusable code block. In C/C++, you can define a multi-line macro by using the backslash (\) at the end of each line to continue the macro definition on the next line. Here’s an example of a multi-line macro that checks whether a number is even or odd.


#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; 
} 

The code will give a compilation error while compiling the code.

Output

[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]$ 

Example-to illustrate macro ends with the semicolon

In C and C++, statements should end with a semicolon. However, when defining a macro, the semicolon at the end of the macro is required. In the provided example, the mistake is not in including the curly braces in the macro, but rather in including the semicolon at the end of the macro. The correct way to define the macro is with the semicolon:

if (num & 1)
{
    -------------------------
    ---- Macro expansion ----
    -------------------------
};    /* Semicolon at the end of MACRO, and here is ERROR */

else 
{
   -------------------------
   ---- Macro expansion ----
   -------------------------

};

Example- to illustrate Modified macro

The placement of a semicolon after the “if” statement when the macro is used in the code. To address this issue and ensure that the macro behaves correctly in all situations, it’s a common practice to enclose the macro in a do-while(0) loop. This is often referred to as the “do-while(0) trick.” The modified macro will look like this:



#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; 
} 

Now, we can compile and run the code above. The output we get is given below :

[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]$ 

In C or C++ programming, when you create a multi-line macro, you might encounter issues with semicolon placement. To avoid this problem, there are two common techniques:

  1. The “do-while(0)” Trick: You can enclose your multi-line macro within a do-while(0) loop. This loop executes only once, and it helps ensure that your macro can be used safely in complex code structures.


#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]$ 

FAQ- Multiline macros in C

Q1.How to create a multiline macro in C?

Ans. In C or C++ macros, each line should end with a backslash (\) for multiline macros. If you use curly braces without proper enclosure, it may lead to errors. To ensure correct behavior, enclose the entire multiline macro within parentheses. This keeps it functioning as a single statement in your code.

Q2. How do you separate a multi-line macro in C language?

Ans. In C, macros allow you to replace one part of code with another. You can create multiline functionality with macros, but instead of using new lines, you use the backslash (‘\’) to separate different parts of the macro.

Q3. What are the two types of macros in C?

Ans. In C, you have two types of macros: function-like macros and object-like macros. Function-like macros are created with a code snippet that can take parameters, while object-like macros are defined with a simple value.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/multiline-macros-in-c/feed/ 0