微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

理解逗号运算符

int main()
{
    int a = (1,2,3);
    int b = (++a,++a,++a);
    int c= (b++,b++,b++);
    printf("%d %d %d",a,b,c);
}

我是编程初学者.我没有得到这个程序如何显示6 9 8的输出.

解决方法

用于所有三个声明
int a = (1,3);
int b = (++a,++a);
int c = (b++,b++);

它是comma operator.它计算第一个操作数1并丢弃它,然后计算第二个操作数并返回其值.因此,

int a = ((1,2),3);          // a is initialized with 3.
int b = ((++a,++a),++a);   // b is initialized with 4+1+1 = 6. 
                             // a is 6 by the end of the statement
int c = ((b++,b++),b++);   // c is initialized with 6+1+1 = 8
                             // b is 9 by the end of the statement.

1在逗号运算符的情况下,从左到右保证评估顺序.

原文地址:https://www.jb51.cc/c/115585.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐