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

CExpressionEvaluationUnary Operator

C Problem Overview


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

int foo = +4;

However, as far as I can tell, the unary plus (+) in +4 is a no-op. Is it?

C Solutions


Solution 1 - C

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 x evaluates to (say) a pointer.

That is about the only practical use I can think of.

Solution 2 - C

As per the C90 standard in 6.3.3.3:

> 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..

Solution 3 - C

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 FOO is undefined, the C language requires it be replaced by 0 in this case. But if FOO was defined with an empty definition, the above directive will result in an error. Instead you can use:

#if FOO+0 > 0

And now, the directive will be syntactically correct whether FOO is undefined, defined as blank, or defined as an integer value.

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 distinguish the cases of FOO being defined as zero versus defined as blank, as in:

#if 2*FOO+1 == 1
/* FOO is 0 */
#else
/* FOO is blank */
#endif

Solution 4 - C

Pretty much. It's mainly present for completeness, and to make constructions like this look a little cleaner:

int arr[] = {
    +4,
    -1,
    +1,
    -4,
};

Solution 5 - C

I found two things that unary + operator do is

  • integer promotion
  • turning 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):

1 2 4 8
4 4 4 8

turning lvalue into rvalue example:

int i=0,j;

j=(+i)++; // error lvalue required

Solution 6 - C

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 -.

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

1
4

Solution 7 - C

>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.

Solution 8 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzneakView Question on Stackoverflow
Solution 1 - CNemoView Answer on Stackoverflow
Solution 2 - ClccarrascoView Answer on Stackoverflow
Solution 3 - CR.. GitHub STOP HELPING ICEView Answer on Stackoverflow
Solution 4 - Cuser149341View Answer on Stackoverflow
Solution 5 - CA.s. BhullarView Answer on Stackoverflow
Solution 6 - CDigitalRossView Answer on Stackoverflow
Solution 7 - CShafik YaghmourView Answer on Stackoverflow
Solution 8 - CDefenestrationDayView Answer on Stackoverflow