Are compound statements (blocks) surrounded by parens expressions in ANSI C?

前端 未结 2 981
臣服心动
臣服心动 2020-11-22 11:45

Browsing the Linux kernel sources I found some piece of code where a block of statements surrounded by parenthesis is treated as a expression a la lisp (or ML), tha

相关标签:
2条回答
  • 2020-11-22 12:18

    This is not standard C. It is a gcc extension called statement expressions. You can find the complete list of C extensions here. This is actually one of the many gcc extensions used in the Linux kernel and it seems like clang supports this too and although it is not explicitly named in the document.

    As you observed the last expression serves as the value of the expression, the document says (emphasis mine):

    The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. (If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.)

    One of the main benefits would be to make safe macros that would avoid multiple evaluations of arguments with side effects. The example given uses this unsafe macro:

    #define max(a,b) ((a) > (b) ? (a) : (b))
    

    which evaluates either a or b twice and can be rewritten to eliminate this problem using statement expressions as follows:

    #define maxint(a,b) \
       ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) 
    

    Note, the need to explicitly use int which can fixed using another gcc extension Typeof:

    #define max(a,b) \
       ({ typeof (a) _a = (a), _b = (b); _a > _b ? _a : _b; }) 
    

    Note that clang also supports typeof.

    0 讨论(0)
  • 2020-11-22 12:20

    It's called "braced-group within expression".

    It's not allowed by ANSI/ISO C nor C++ but gcc supports it.

    0 讨论(0)
提交回复
热议问题