What is the purpose of the unary plus (+) operator in C?

前端 未结 8 2057
攒了一身酷
攒了一身酷 2020-11-28 05:58

In C, it\'s legal to write something like:

int foo = +4;

However, as far as I can tell, the unary plus (+) in +4

相关标签:
8条回答
  • 2020-11-28 06:47

    What is the purpose of the unary '+' operator in C?

    Unary plus was added to C for symmetry with unary minus, from the Rationale for International Standard—Programming Languages—C:

    Unary plus was adopted by the C89 Committee from several implementations, for symmetry with unary minus.

    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.3 Unary arithmetic operators says:

    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:

    Unary plus is a historical accident and generally useless.

    0 讨论(0)
  • 2020-11-28 06:48

    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.

    0 讨论(0)
提交回复
热议问题