In what versions of C is a block inside parenthesis used to return a value valid?

后端 未结 3 727
忘掉有多难
忘掉有多难 2020-11-28 14:11

If I do:

int j = ({int x = 7; x+3;});

In i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) gcc it compiles just fine. The

3条回答
  •  独厮守ぢ
    2020-11-28 14:58

    You can expect it to work in most versions of GCC.

    You can expect it to work almost nowhere else - it is a GCC extension.

    The section of the GCC manual that describes the feature is titled 'Statements and Declarations in Expressions':

    A compound statement enclosed in parentheses may appear as an expression in GNU C.

    Later it says:

    Any temporaries created within a statement within a statement expression will be destroyed at the statement’s end. This makes statement expressions inside macros slightly different from function calls.

    So, 'statement expression' seems to be the name used in the documentation.

提交回复
热议问题