Using clang, the code actually compiles, but it gives some warnings. The warnings are generated because of the parenthesis and braces in the macro definition, or – to be more exact – because of the lack of braces in the if-else statement. You can rewrite the code to the following form:
#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;
}
You can further simplify your code to:
#include <stdio.h>
#define MACRO(num, str) printf("%d is %s number\n", num, str);
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num & 1)
{
MACRO(num, "Odd");
}
else
{
MACRO(num, "Even");
}
return 0;
}
solved Why this code gives error? It uses parenthesis in multi line macros.I found this on an article in GeeksforGeeks