16

How do you define a multiline macro in C?

rocambille
  • 15,398
  • 12
  • 50
  • 68
Luis B
  • 1,684
  • 3
  • 15
  • 25

3 Answers3

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;
}
shriroop_
  • 775
  • 6
  • 15
5

Use \ to escape line return:

#define MULTILINE_MACRO()\
    line1\
    line2
rocambille
  • 15,398
  • 12
  • 50
  • 68
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\
}\
}
Luis B
  • 1,684
  • 3
  • 15
  • 25