C语言 C 中一元加号 (+) 运算符的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6637005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of the unary plus (+) operator in C?
提问by zneak
In C, it's legal to write something like:
在 C 中,写这样的东西是合法的:
int foo = +4;
However, as far as I can tell, the unary plus (+) in +4is a no-op. Is it?
但是,据我所知,一元加号 ( +)+4是空操作。是吗?
采纳答案by lccarrasco
As per the C90 standard in 6.3.3.3:
根据 6.3.3.3 中的 C90 标准:
The result of the unary + operator is the value of its operand. The integral promotion is performed on the operand. and the result has the promoted type.
一元 + 运算符的结果是其操作数的 值。对操作数执行积分提升。结果具有提升的类型。
and
和
The operand of the unary + or - operator shall have arithmetic type..
一元 + 或 - 运算符的操作数应具有 算术类型..
回答by Nemo
You can use it as a sort of assertion that an expression has arithmetic type:
您可以将其用作表达式具有算术类型的断言:
#define CHECK_ARITHMETIC(x) (+(x))
This will generate a compile-time error if xevaluates to (say) a pointer.
如果x计算结果为(比如说)一个指针,这将产生一个编译时错误。
That is about the only practical use I can think of.
这是我能想到的唯一实际用途。
回答by R.. GitHub STOP HELPING ICE
There's one very handy use of the unary plus operator I know of: in macros. Suppose you want to do something like
我所知道的一元加运算符有一个非常方便的用法:在宏中。假设你想做类似的事情
#if FOO > 0
If FOOis undefined, the C language requires it be replaced by 0 in this case. But if FOOwas defined with an empty definition, the above directive will result in an error. Instead you can use:
如果FOO未定义,则在这种情况下,C 语言要求将其替换为 0。但是如果FOO定义为空定义,则上述指令将导致错误。相反,您可以使用:
#if FOO+0 > 0
And now, the directive will be syntactically correct whether FOOis undefined, defined as blank, or defined as an integer value.
现在,无论FOO是未定义、定义为空白还是定义为整数值,指令在语法上都是正确的。
Of course whether this will yield the desired semantics is a completely separate question, but in some useful cases it will.
当然,这是否会产生所需的语义是一个完全不同的问题,但在某些有用的情况下会。
Edit:Note that you can even use this to distinguishthe cases of FOObeing defined as zero versus defined as blank, as in:
编辑:请注意,你甚至可以用它来区分的情况下,FOO被定义为与零定义为空白,如:
#if 2*FOO+1 == 1
/* FOO is 0 */
#else
/* FOO is blank */
#endif
回答by duskwuff -inactive-
Pretty much. It's mainly present for completeness, and to make constructions like this look a little cleaner:
差不多。它主要是为了完整性而存在,并使这样的结构看起来更简洁:
int arr[] = {
+4,
-1,
+1,
-4,
};
回答by DigitalRoss
Not precisely a no-op
不完全是空操作
The unary +operator does only one thing: it applies the integer promotions. Since those would occur anyway if the operand were used in an expression, one imagines that unary +is in C simply for symmetry with unary -.
一元运算+符只做一件事:它应用整数提升。由于如果在表达式中使用操作数,无论如何这些都会发生,因此人们认为一元+在 C 中只是为了与一元对称-。
It's difficult to see this in action because the promotions are so generally applied.
很难看到这一点,因为促销是如此普遍。
I came up with this:
我想出了这个:
printf("%zd\n", sizeof( (char) 'x'));
printf("%zd\n", sizeof(+(char) 'x'));
which (on my Mac) prints
哪个(在我的 Mac 上)打印
1
4
回答by A.s. Bhullar
I found two things that unary +operator do is
我发现一元运算+符做的两件事是
integer promotionturning lvalue into rvalue
integer promotionturning lvalue into rvalue
integer promotion example:
整数提升示例:
#include <stdio.h>
int main(void) {
char ch;
short sh;
int i;
long l;
printf("%d %d %d %d\n",sizeof(ch),sizeof(sh),sizeof(i),sizeof(l));
printf("%d %d %d %d\n",sizeof(+ch),sizeof(+sh),sizeof(+i),sizeof(+l));
return 0;
}
Typical output (on 64-bit platform):
典型输出(在 64 位平台上):
1 2 4 8
4 4 4 8
turning lvalue into rvalue example:
将左值转换为右值示例:
int i=0,j;
j=(+i)++; // error lvalue required
回答by Shafik Yaghmour
What is the purpose of the unary '+' operator in C?
C 中一元“+”运算符的用途是什么?
Unary plus was added to C for symmetry with unary minus, from the Rationale for International Standard—Programming Languages—C:
一元加号被添加到 C 以与一元减号对称,来自国际标准的基本原理 - 编程语言 - C:
Unary plus was adopted by the C89 Committee from several implementations, for symmetry with unary minus.
C89 委员会从几个实现中采用了一元加号,以与一元减号对称。
and it is not a no-op, it performs the integer promotions on its operand. Quoting from my answer to Does Unary + operator do type conversions?:
它不是空操作,它在其操作数上执行整数提升。引用我对一元 + 运算符是否进行类型转换的回答?:
The draft C99 standard section 6.5.3.3Unary arithmetic operatorssays:
C99标准部分草案6.5.3.3一元算术运算符说:
The result of the unary + operator is the value of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.
一元 + 运算符的结果是其(提升的)操作数的值。对操作数执行整数提升,结果具有提升的类型。
Worth pointing out that Annotated C++ Reference Manual(ARM)provides the following commentary on unary plus:
值得指出的是Annotated C++ Reference Manual ( ARM)对一元加号提供了以下评论:
Unary plus is a historical accident and generally useless.
一元加号是一个历史意外,一般没有用。
回答by DefenestrationDay
By 'no-op', do you mean the assembly instruction?
If so, then definitely not.
“无操作”是指汇编指令吗?
如果是这样,那绝对不是。
+4 is just 4- the compiler won't add any further instructions.
+4 只是 4- 编译器不会添加任何进一步的指令。

