Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How do you define a multiline macro in C?

like image 679
Luis B Avatar asked Oct 02 '16 20:10

Luis B


People also ask

How is multiline macro defined?

We can write multiline macros like functions, but for macros, each line must be terminated with backslash '\' character. If we use curly braces '{}' and the macros is ended with '}', then it may generate some error. So we can enclose the entire thing into parenthesis.

Which symbol is used to define multiline macro in C?

How do you define a multiline macro in C? Use '\' at line endings.

What is nested macro in C?

A nested macro instruction definition is a macro instruction definition you can specify as a set of model statements in the body of an enclosing macro definition. This lets you create a macro definition by expanding the outer macro that contains the nested definition.

Can you define a macro as another macro C?

You cannot define macros in other macros, but you can call a macro from your macro, which can get you essentially the same results.


2 Answers

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;
}
like image 127
shriroop_ Avatar answered Oct 01 '22 19:10

shriroop_


Use \ to escape line return:

#define MULTILINE_MACRO()\
    line1\
    line2
like image 42
rgmt Avatar answered Oct 01 '22 19:10

rgmt