How does this code work?

Possible duplicate:
Return value from local scope?

#include <stdio.h>

int main() {
        int x = ({int a = 2; a;});
        printf("%d\n", x);
}

Output: 2

+3
source share
3 answers

This is a non-standard extension to the C / C ++ languages ​​provided by GCC, called expression expressions. If you try to compile the checkbox -pedantic, you will get the warning "ISO C forbids grouping groups in expressions." This portable code is not , and should be avoided if possible.

+15
source

({ ... }) - GCC . , .

+2

It declares a local variable named 'a', assigns the value '2' to it, and then assigns the value x.

+1
source

Source: https://habr.com/ru/post/1780758/


All Articles