Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define and expected primary-expression error

The following code causes "expected primary-expression before ‘)’ token" error when trying to compile it on Linux:

#define PRINTF(args, ...) printf((args), __VA_ARGS__)

void test( )
{
   PRINTF( "test" );
}

The same code works fine on Windows. I am not sure what the problem is.

Solution:

adding ## before __VA_ARGS__ solves the problem

like image 663
darkThoughts Avatar asked Mar 05 '26 07:03

darkThoughts


1 Answers

In Standard C, #define PRINTF(args, ...) means that any invocation of the PRINTF macro must supply at least two arguments.

The GNU preprocessor offers two extensions:

  • It is possible to supply one argument in this case, and it will behave as if a second, empty argument had been provided.
  • , ## __VA_ARGS__ is allowed in the expansion with meaning:
    • If the empty argument extension from the previous bullet point is happening, this all expands to nothing (i.e. no trailing comma)
    • Otherwise, it behaves as , __VA_ARGS__.

If you see a compiler accept your original code, it means that there is a preprocessor in use that offers non-standard extensions. Conversely, your "solution" might not work on some implementations.

like image 63
M.M Avatar answered Mar 06 '26 20:03

M.M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!