user-0 提问: C 语言中，关于 ( ) 运算符的解释

回复:
user: user-1
content: 在 for 语句中，关键字 for 后面的圆括号以及被括住的部分并不是语句，更不是复合语句，它只是组成 for 语句的一个成份：  for (声明/表达式1； 表达式2； 表达式3） 语句  下面这个程序，在标准C中是非法的、不可思议的，但它就是可以用 GNU C 编译并正常运行： # include <stdio.h>  int main (void) {     int n = 1, sum = 0;     printf ("%d\n", ({for (; n <= 100;) sum += n ++; sum;})); }

user: user-1
content: 引用 6 楼 十敷 的回复:[quote=引用 3 楼 均陵鼠侠的回复:]这种写法在标准的C和C++中都是不合法的，属于 GNU C 的扩展。在 GNU C 中，可以用一对圆括号将复合语句括起来，如此一来，你就可以在表达式中使用循环语句、选择语句和局部变量。  具体的说明，详见 GCC 的这篇文档：https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Statement-Exprs.html 那这么说来 for 循环也是gnuc 拓展嘛？ [/quote] for 怎么是 GNU C 的扩展呢？说说你的理由。

user: user-2
content: 引用 3 楼 均陵鼠侠的回复:这种写法在标准的C和C++中都是不合法的，属于 GNU C 的扩展。在 GNU C 中，可以用一对圆括号将复合语句括起来，如此一来，你就可以在表达式中使用循环语句、选择语句和局部变量。  具体的说明，详见 GCC 的这篇文档：https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Statement-Exprs.html 那这么说来 for 循环也是gnuc 拓展嘛？

user: user-2
content: 引用 7 楼 均陵鼠侠的回复:[quote=引用 6 楼 十敷 的回复:][quote=引用 3 楼 均陵鼠侠的回复:]这种写法在标准的C和C++中都是不合法的，属于 GNU C 的扩展。在 GNU C 中，可以用一对圆括号将复合语句括起来，如此一来，你就可以在表达式中使用循环语句、选择语句和局部变量。  具体的说明，详见 GCC 的这篇文档：https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Statement-Exprs.html 那这么说来 for 循环也是gnuc 拓展嘛？ [/quote] for 怎么是 GNU C 的扩展呢？说说你的理由。[/quote] 第一 用小括号括的复合语句 第二 在括号里可以用局部变量 其实我都不大了解啥是gnu 只是看到您的解释突发奇想

user: user-0
content: 引用 3 楼 均陵鼠侠 的回复:这种写法在标准的C和C++中都是不合法的，属于 GNU C 的扩展。在 GNU C 中，可以用一对圆括号将复合语句括起来，如此一来，你就可以在表达式中使用循环语句、选择语句和局部变量。  具体的说明，详见 GCC 的这篇文档：https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Statement-Exprs.html 感谢大哥解惑

user: user-1
content: 这种写法在标准的C和C++中都是不合法的，属于 GNU C 的扩展。在 GNU C 中，可以用一对圆括号将复合语句括起来，如此一来，你就可以在表达式中使用循环语句、选择语句和局部变量。  具体的说明，详见 GCC 的这篇文档：https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Statement-Exprs.html

user: user-3
content: Order of Evaluation This section discusses the order in which expressions are evaluated but does not explain the syntax or the semantics of the operators in these expressions. The earlier sections in this chapter provide a complete reference for each of these operators.  Expressions are evaluated according to the precedence and grouping of their operators. (Table 1.1 in Chapter 1, Lexical Conventions, shows the relationships the C++ operators impose on expressions.) Consider this example:  #include <iostream.h>  void main() {     int a = 2, b = 4, c = 9;      cout << a + b * c << "\n";     cout << a + (b * c) << "\n";     cout << (a + b) * c << "\n"; }  The output from the preceding code is:  38 38 54  Figure 4.1   Expression-Evaluation Order     The order in which the expression shown in Figure 4.1 is evaluated is determined by the precedence and associativity of the operators:   Multiplication (*) has the highest precedence in this expression; hence the subexpression b * c is evaluated first.   Addition (+) has the next highest precedence, so a is added to the product of b and c.   Left shift (<<) has the lowest precedence in the expression, but there are two occurrences. Because the left-shift operator groups left-to-right, the left subexpression is evaluated first and then the right one.  When parentheses are used to group the subexpressions, they alter the precedence and also the order in which the expression is evaluated, as shown in Figure 4.2.  Figure 4.2   Expression-Evaluation Order with Parentheses    Expressions such as those in Figure 4.2 are evaluated purely for their side effects — in this case, to transfer information to the standard output device.  Note   The left-shift operator is used to insert an object in an object of class ostream. It is sometimes called the “insertion” operator when used with iostream.

user: user-3
content: Expression Statement Expression statements cause expressions to be evaluated. No transfer of control or iteration takes place as a result of an expression statement.  Syntax  expression-statement:  expressionopt ;  All expressions in an expression statement are evaluated and all side effects are completed before the next statement is executed. The most common expression statements are assignments and function calls. C++ also provides a null statement.    {语句1;语句2;} //将两个语句组合成一个语句 Compound Statements (Blocks) A compound statement consists of zero or more statements enclosed in curly braces ({ }). A compound statement can be used anywhere a statement is expected. Compound statements are commonly called “blocks.”  Syntax  compound-statement :  { statement-listopt }  statement-list :  statement statement-list  statement  The following example uses a compound statement as the statement part of the if statement (see The if Statement for details about the syntax):  if( Amount > 100 ) {     cout << "Amount was too large to handle\n";     Alert(); } else     Balance -= Amount;  Note   Because a declaration is a statement, a declaration can be one of the statements in the statement-list. As a result, names declared inside a compound statement, but not explicitly declared as static, have local scope and (for objects) lifetime. See Scope in Chapter 2 for details about treatment of names with local scope.    (语句) //将语句当作表达式计算其值 C++ Operators Operators specify an evaluation to be performed on one of the following:   One operand (unary operator)   Two operands (binary operator)   Three operands (ternary operator)  The C++ language includes all C operators and adds several new operators. Table 1.1 lists the operators available in Microsoft C++.  Operators follow a strict precedence which defines the evaluation order of expressions containing these operators. Operators associate with either the expression on their left or the expression on their right; this is called “associativity.” Operators in the same group have equal precedence and are evaluated left to right in an expression unless explicitly forced by a pair of parentheses, ( ).