简体   繁体   English

C运算符与评估

[英]C operators and evaluation

#include<stdio.h>
int main()
{
int i=3,val;
val=sizeof (f(i))+ +f(i=1)+ +f(i-1);
printf("%d%d",val,i);
getch();
return 0;

}
int f(int num)
{
return num*5;
}

The compiler compiles the program and gives the output 7 1 ..what do the " + +" mean??? 编译器编译程序并给出输出7 1 ..“ ++”是什么意思???

First note that sizeof is compile-time evaluable so the first term in val will be sizeof(int) : int being the return type of f . 首先请注意sizeof是可在编译时求值的,因此val的第一项将是sizeof(int)intf的返回类型。

The value of the entire expression that you want to assign to val is undefined since + as a binary and unary operator is not sequenced. 您想要分配给val的整个表达式的值是不确定的,因为+作为二进制和一元运算符没有顺序。 In essence you don't know if i = 1 will happen before or after the evaluation of i - 1 . 本质上,您不知道i = 1是在i - 1评估之前还是之后发生。

As for your specifics, a + + b is evaluated as a + (+b) . 至于您的具体情况, a + + b被评估为a + (+b) +b is simply a unary plus ( almost a no-op, but does do some subtle type coercion), the other + is the addition operator taking two arguments. +b只是一元加号( 几乎是无操作符,但确实具有一些微妙的类型强制),另一个+是采用两个参数的加法运算符。

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

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