简体   繁体   English

关于 C 中运算符优先级的问题

[英]Question regarding operator precedence in C

I'm starting to learn to program in c, and I thought I was already pretty confident with the precedence of operators, until I did this:我开始学习在 c 中编程,我以为我已经对运算符的优先级很有信心,直到我这样做了:

a > b ? c = a : (c = b);

Of course at the first time I didn't use parenthesis on the last sentence, but since that ended up causing compiling issues I searched how to solve that problem on this forum and I read that adding parenthesis could do the job.当然,第一次我没有在最后一句话上使用括号,但由于这最终导致了编译问题,我在这个论坛上搜索了如何解决这个问题,我读到添加括号可以完成这项工作。 However, I thought that the expressions inside parenthesis get executed before anything else written in the same line, which would mean that the c = b sentence was executed first and then the ternary operator.但是,我认为括号内的表达式会在同一行中的其他任何内容之前执行,这意味着先执行c = b语句,然后执行三元运算符。 I did something similar but easier to read in order to get a better idea of what was happening with this operator precedence thing and tried executing this line:我做了一些类似但更容易阅读的事情,以便更好地了解这个运算符优先级发生了什么,并尝试执行这一行:

printf("Number is %d", i) + (i = 5);

I know this expression returns returns a value, but since I don't need it and this isn't a line that I will keep for more than 5 seconds, I won't store it in any variable.我知道这个表达式返回一个值,但是因为我不需要它,而且这不是我将保留超过 5 秒的行,所以我不会将它存储在任何变量中。 What gets my attention in this case is that, when I execute the code, I doesn't show up on the screen with the value 5, but instead it uses the previous value, which means that the computer is just reading it from left to right.在这种情况下,引起我注意的是,当我执行代码时,我并没有以值 5 显示在屏幕上,而是使用以前的值,这意味着计算机只是从左到右读取它正确的。 When I do:当我做:

(i = 5) + printf(Numer is %d, i);

it first does the assignment of i and only after that the printf function is executed.它首先进行 i 的分配,然后才执行 printf function 。 My question is: how does the computer execute an expression that uses operators of different orders of precedence?我的问题是:计算机如何执行使用不同优先顺序的运算符的表达式? It clearly doesn't run first the operator with the highest precedence, because in the first printf the value stored wasn't the one assigned on the parenthesis, but it also doesn't just read from left to right because in that case there would be no operator precedence.它显然不会首先运行具有最高优先级的运算符,因为在第一个 printf 中,存储的值不是分配在括号中的值,但它也不只是从左到右读取,因为在这种情况下会有没有运算符优先级。 How does it work?它是如何工作的?

Parenthesis and operator precedence only dictate how operands are grouped.括号和运算符优先级仅指示操作数的分组方式。 It does not dictate the order of evaluation .它不规定评估的顺序

In this expression:在这个表达式中:

a > b ? c = a : (c = b);

The three parts of the ternary operator are a > b , c = a , and c = b respectively.三元运算符的三个部分分别是a > bc = ac = b This operator also has the property that only one of the second and third clause are evaluated, based on the result of the first.该运算符还具有根据第一个子句的结果仅评估第二个和第三个子句中的一个的属性。 Formally speaking, there is a sequence point between the evaluation of the first clause and of either the second or third.从形式上讲,在第一个子句的评估与第二个或第三个子句的评估之间存在一个顺序点 So a > b is first evaluated.所以首先评估a > b If it is nonzero, c = a is evaluated, otherwise c = b is evaluated.如果它不为零,则评估 c = c = a ,否则评估c = b

In this expression:在这个表达式中:

printf("Number is %d", i) + (i = 5); 

There is nothing that dictates whether printf("Number is %d", i) or i = 5 is evaluated first.没有什么可以决定是否首先评估printf("Number is %d", i)i = 5 Unlike the ternary operator, there is no sequence point between the evaluation of the operands of the + operator.与三元运算符不同, +运算符的操作数的求值之间没有序列点。 This expression also has a problem: i is both read and written in the same expression without a sequence point.这个表达式也有一个问题: i在没有序列点的同一个表达式中被读取和写入。 Doing so triggers undefined behavior .这样做会触发未定义的行为 This is also true for:这也适用于:

(i = 5) + printf(Numer is %d, i);

On a side note, this:顺便说一句,这是:

a > b ? c = a : (c = b);

Can be more clearly written as:可以更清楚地写成:

c = a > b ? a : b;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM