I recently saw a piece of C code including a macro of the following style:
#define TOTO()              \
do {                        \
  do_something();           \
  do_another_something();   \
} while (0)
I was at first wondering of the purpose of the do while (0) here, but this answer explained to me: it is in case the macro is used just after an if or else without curly brackets, like this:
if (something)
    TOTO();
else
    do_something_else();
So here, without the do while (0) statement, the code would be expanded to:
if (something)
    do_something();
    do_another_something();
else
    do_something_else();
Which is syntactically wrong, because the else is not directly following an if scope anymore.
But I thought it would work as well by declaring the macro in its own scope, without necessary a do while around it, so I tested the same code with just curly brackets. My entire code looks like this:
#include <stdio.h>
#define HELLO_WORLD()       \
{                           \
    printf("hello ");       \
    printf("world!\n");     \
}
int     main(int argc, char** argv)
{
    if (argc == 1)
        HELLO_WORLD();
    else
        fprintf(stderr, "nope\n");
    return 0;
}
But GCC give me the following error:
error: ‘else’ without a previous ‘if’
However, the code of the main function should be expanded to:
if (argc == 1)
    {
        printf("hello ");
        printf("world!\n");
    }
else
    fprintf(stderr, "nope\n");
return 0;
Which is valid.
So what am I missing here?
 
     
     
    