News for hackers. Stuff that matters.
Re:C23: A Slightly Better C by kstrauser on Sunday January 21, 2024 @03:53PM (#39082925)
Why’s that inside do/while?
Re:C23: A Slightly Better C by trealira on Sunday January 21, 2024 @04:13PM (#39083129)
If you simply put braces around it, you'd generate syntactically invalid code when you don't use braces for if-else statements.

Consider this:

  if (e)
      SWAP(a, b);
  else
     something_else();
Currently, that expands to this, which is still valid code:

  if (e)
      do {
          // ...macro...
      } while(0);
  else
      something_else();
If it were just wrapped in braces, the code would be parsed like this:

  // One-armed if-statement
  if (e) {
      // ...macro...
  }

  // Empty statement
  ;

  // Another statement
  else something_else();
It's incorrect syntax to start a statement with else, so the compiler will say something like "Unexpected 'else' at line N."
Re:C23: A Slightly Better C by kstrauser on Sunday January 21, 2024 @04:57PM (#39083501)
I would not have come up with that on my own. Thanks for the explanation!
Re:C23: A Slightly Better C by trealira on Sunday January 21, 2024 @05:15PM (#39083639)
No problem! I didn't come up with it on my own, either. I simply read it somewhere, and now I'm repeating it.
Re:C23: A Slightly Better C by glouwbug on Sunday January 21, 2024 @03:59PM (#39082992)
Forces the use of a semicolon:

SWAP(…);

Re:C23: A Slightly Better C by asplake on Sunday January 21, 2024 @03:57PM (#39082974)
To scope the temporaries?
Re:C23: A Slightly Better C by peterfirefly on Sunday January 21, 2024 @05:30PM (#39083744)
No, a block would be enough for that. It's to make ; handling natural when the macro is used.
Re:C23: A Slightly Better C by _kst_ on Sunday January 21, 2024 @05:06PM (#39083572)
https://www.c-faq.com/ question 10.4

"What's the best way to write a multi-statement macro?"