int main(){
int x = 5580, y = 55098;
printf( "%d\n", y = x);
return 0;
}
为什么会打印 y 的值?我以为 x 的值被分配给 y 但事实并非如此。
最佳答案
根据standard ,赋值表达式的计算结果为左操作数赋值后的值:
6.5.16 Assignment operators
(3) An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment
因此 printf("%d\n", y = x)
中的 y=x
首先让 y
取以下值x
,然后计算出 y
的(新分配的)值。因此,printf
语句的输出将是5580
,即x
的值,它是y<的新值
.
关于c - C中赋值运算符和等于运算符有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663229/