How do you define a multiline macro in C? [duplicate]
Asked Answered
B

3

16

How do you define a multiline macro in C?

Bryology answered 2/10, 2016 at 20:6 Comment(1)
Use '\' at line endings.Mehitable
O
28

End every line of definition of macro with a \

#include <stdio.h>
#define MAX(a,b) {\
    printf("%d ", a); \
    printf("%d\n", b); \
}

int main()
{
    printf("Hello, World!\n");
    MAX(4, 5);
    return 0;
}
Octastyle answered 2/10, 2016 at 20:13 Comment(0)
G
6

Use \ to escape line return:

#define MULTILINE_MACRO()\
    line1\
    line2
Guanabana answered 2/10, 2016 at 20:10 Comment(0)
B
3

Below are two C macros that are multiline

#define for_loop_begin(size)\
for (int i = 0; i < size; i++) \
{ \
for (int j = 0; j < size; j++) {


#define for_loop_end\
}\
}
Bryology answered 2/10, 2016 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.