【问题标题】:comma separated assignment to an integer variable n c [duplicate]对整数变量 n c 的逗号分隔赋值 [重复]
【发布时间】:2015-05-26 22:38:36
【问题描述】:

我无法理解这段代码的工作原理。

#include<stdio.h>
void main(){
  int a,b;
  a=3,1;
  b=(5,4);
  printf("%d",a+b);
}  

输出为7。那是什么任务?

【问题讨论】:

  • 不要在现实生活中这样做!另外,请使用空格。
  • 注意,我意识到了这个问题,虽然规范没有解决这个特定问题,所以我更新了我对上面链接问题的回答,也涵盖了这个问题。
  • 如果你使用调试器,你会看到分配给ab的值,这将有助于你理解。如果你开始向别人征求这样的想法,你的进步会很慢。

标签: c variable-assignment comma-operator


【解决方案1】:

逗号运算符计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回此值。

执行后

a=3,1;  //  (a = 3), 1;

a 将有3 及之后

b=(5,4);  // Discard 5 and the value of the expression (5,4) will be 4

b 将拥有4

Wikipedia 上的更多示例:

// Examples:               Descriptions:                                                                   Values after line is evaluated:
int a=1, b=2, c=3, i=0; // commas act as separators in this line, not as an  operator 
                        // ... a=1, b=2, c=3, i=0
i = (a, b);             // stores b into i 
                        // ... a=1, b=2, c=3, i=2
i = a, b;               // stores a into i. Equivalent to (i = a), b;
                        // ... a=1, b=2, c=3, i=1
i = (a += 2, a + b);    // increases a by 2, then stores a+b = 3+2 into i
                        // ... a=3, b=2, c=3, i=5
i = a += 2, a + b;      // increases a by 2, then stores a to i, and discards unused
                        // a + b rvalue. Equivalent to (i = (a += 2)), a + b; 
                        // ... a=5, b=2, c=3, i=5
i = a, b, c;            // stores a into i, discarding the unused b and c rvalues
                        // ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i, discarding the unused a and b rvalues
                        // ... a=5, b=2, c=3, i=3
return a=4, b=5, c=6;   // returns 6, not 4, since comma operator sequence points
                        // following the keyword 'return' are considered a single
                        // expression evaluating to rvalue of final   subexpression c=6
return 1, 2, 3;         // returns 3, not 1, for same reason as previous example
return(1), 2, 3;        // returns 3, not 1, still for same reason as above.  This
                        // example works as it does because return is a keyword, not 
                        // a function call. Even though most compilers will allow for
                        // the construct return(value), the parentheses are syntactic
                        // sugar that get stripped out without syntactic analysis 

【讨论】:

    【解决方案2】:

    在此声明中

    a=3,1;
    

    使用了两个运算符:赋值运算符和逗号运算符。赋值运算符的优先级大于逗号运算符的优先级,因此该语句等价于

    ( a = 3 ), 1;
    

    1 被简单地丢弃,所以a 被赋值为3

    在此声明中

    b=(5,4);
    

    由于括号,首先计算逗号运算符。它的值是最后一个表达式4 的值。所以b 被赋值为4

    结果你得到a + b =&gt; 3 + 4,它等于7

    【讨论】:

      【解决方案3】:
        a=3,1; // (a=3),1 -- value of expression is 1, side effect is changing a to 3
        b=(5,4);
        printf("%d",a+b); // 3 + 4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-30
        • 2011-07-26
        • 2013-07-05
        • 2021-11-08
        • 1970-01-01
        • 2017-07-04
        • 1970-01-01
        • 2012-10-22
        相关资源
        最近更新 更多