More information on `({});` in C?

后端 未结 3 1214
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 00:58

I\'ve noticed that sometimes, C macros are written as something like this:

#define foo(bar) ({ ++bar; })

After some experimentation, I\'ve

3条回答
  •  日久生厌
    2021-01-18 01:46

    This is a GNU extension called statement expressions.

    When declaring macros in standard-C, you often see do...while(0) loops used for similar purposes (ie creating a block scope). A statement expression is superior to the loop hack because it can return a value. If you want to do something similar in standard-C, you'd have to define an additional function and lose the convenience of lexical scoping.

提交回复
热议问题