0

I know that it is commonly used in macros:

#define MY_MACRO(x)  ({ int var = x; /* do stuff */; var; })
...
y = MY_MACRO(43);

But how exactly is it interpreted by the compiler? To my understanding, parentheses in C are used for:

  1. Isolation (of conditions)

  2. Group expressions (for precedence)

  3. Function indication

  4. Casting

I don't understand how any of the above is applied here on the scope block but apparently it does the "magic":

#include <stdio.h>

int main(void)
{
    int a;

    // a = {3};  // --> compilation error "expected expression before '{' token"
    // a = {3;}; // --> Same error
    a = ({3;});  // --> all is good!
    a = ({0; 1; 2; 3;});  // --> warnings for unused values but still, all is good!

    printf("a=%d\n", a); // prints a=3

    return 0;
}
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45

0 Answers0