运算符优先级 - 示例 [英] Operator Precedence - sample

查看:83
本文介绍了运算符优先级 - 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 main(){
int i,j,k;
k =(i = 4 ,j = 5 );
printf( k =%d,k);
return 0 ;
}





哪个获得了优先权以及如何获得?

解决方案

< blockquote>输出 k = 5 因为:

  1. 编译告诉我。
  2. 这是怎么回事逗号运算符工作,即:[逗号]是一个二进制运算符,它计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)(参见维基百科的Comma运营商 [ ^ ])。
  3. 因此评估步骤为:

    1. i = 4 (丢弃)
    2. j = 5 (已退回)
    3. k = 5 k 获取逗号运算符的返回值)


转到此链接,你会知道 pr-order ecedence-in-c-programming-language [ ^ ]


main(){
     int i,j,k;
     k = (i = 4, j = 5);
     printf("k = %d", k);
     return 0;
}



which gets the prcedence and how ?

解决方案

output is k = 5 because:

  1. Compiler told me.
  2. This is how comma operator works, namely: "[comma] is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)" (see Comma operator at Wikipedia[^]).
  3. Hence evaluation steps are:

    1. i = 4 (discarded)
    2. j = 5 (returned)
    3. k = 5 (k gets the returned value of the comma operator)


Go to this link and you will know the order-of-precedence-in-c-programming-language[^]


这篇关于运算符优先级 - 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆