Search code examples
cgccgnu

Braced-group within expression returns "statement with no effect"


I want to use the braced-group within expression, which is a GNU extension to C. The compiler will evaluate the whole block and use the value of the last statement contained in the block. The following code will print 5, but when I compile it with gcc it returns a warning

warning: statement with no effect [-Wunused-value]

typeof(5) x = ({1; 2;}) + 3; // The warning points to "1"
printf("%d\n", x);

Why would the GCC compiler return a warning if this expression was made by GNU?


Solution

  • Why would the GCC compiler return a warning if this expression was made by GNU?

    gcc doesn't complain about the GNU extension itself.

    Rather the statement 1; is useless since the statement expression's result would be the same without it since the final result of ({1; 2;}) is 2.

    i.e. it's equivalent to:

    typeof(5) x = ({2;}) + 3;
    

    so gcc warns that the statement 1; has no effect.