90

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?

8
  • Not exactly the same, but related: stackoverflow.com/questions/727516/…
    – jglouie
    Jul 9, 2011 at 19:33
  • 5
    msdn.microsoft.com/en-us/library/s50et82s.aspx "The unary plus operator preceding an expression in parentheses forces the grouping of the enclosed operations. It is used with expressions involving more than one associative or commutative binary operator. The operand must have arithmetic type. The result is the value of the operand. An integral operand undergoes integral promotion. The type of the result is the type of the promoted operand."
    – Tim S.
    Jul 9, 2011 at 19:36
  • 6
    K&R says it was just added for symmetry in the standard. Jul 10, 2011 at 2:44
  • 2
    @Jeremy: there is. E.g. it says that +short(1) has type int, not short.
    – MSalters
    Jul 11, 2011 at 10:19
  • 1
    @TimS.: "The unary plus operator preceding an expression in parentheses forces the grouping of the enclosed operations" -- Oh? It's the parentheses, not the +, that forces the grouping. Jun 30, 2013 at 22:24

8 Answers 8

83

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.

16
  • 7
    You can also cast an enum value to its integer value this way.
    – GManNickG
    Jul 9, 2011 at 21:14
  • 2
    Why can't you do the same thing with, for example, (-(-(x)))? Jul 10, 2011 at 1:06
  • 8
    @luxun @zneak: Interesting idea... Let me see... OK, how about this. If int is 32-bit and x happens to be an int equal to -2^31, then -x will overflow a signed integer which is technically Undefined Behavior. :-)
    – Nemo
    Jul 10, 2011 at 3:40
  • 4
    @zneak: The point is to trigger a compile time error if the expression has pointer type, but to be a no-op if it has arithmetic type. Your version does fine, except for arithmetic expressions that happen to evaluate to INT_MIN. (Well, in theory, anyway. In practice it probably works fine on any realistic machine.) Still, a strict reading of the standard says these are different.
    – Nemo
    Jul 10, 2011 at 5:34
  • 1
    @zneak: Even if you do not use it in an assignment, merely to evaluate -x when x is INT_MIN is technically to invoke undefined behavior, I believe.
    – Nemo
    Jul 10, 2011 at 6:31
42

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

6
  • 2
    So +x is a noop unless sizeof x < sizeof(int)?
    – zneak
    Jul 9, 2011 at 19:45
  • 36
    These quotes from the standard show that the unary + is not simply a no-op. It does perform integral promotion on the operand. And, possibly more importantly, it does turn an lvalue into an rvalue. Jul 9, 2011 at 19:48
  • Well, it's theoretically possible that sizeof(short) == sizeof(int) but a short has padding, and theoretically, on such a system the padding might need to be zeroed or sign extended. Theoretically. Jul 9, 2011 at 19:49
  • Note that when it says arithmetic type it refers to both Integral types and Floating types, the example Nemo shows works because pointers are outside this classification. Integral types and pointers form Scalar types.
    – lccarrasco
    Jul 9, 2011 at 19:55
  • This doesn't affect a float or a double at all, does it?
    – S.S. Anne
    Jan 13, 2020 at 22:28
38

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
13
  • 1
    Are you sure these preprocessor directives really relate to the unary + operator as seen in C? In you second example, with a blank FOO, the expression wouldn't be valid C.
    – zneak
    Jul 9, 2011 at 23:45
  • 2
    Indeed it does. In any case, the pair of operators - - (space essential!) is equivalent to + as far as I can tell, making + rather redundant... Nope, that's wrong, it's not equivalent when the operand is INT_MIN... :-) Jul 10, 2011 at 2:49
  • 2
    Sorry for bumping such an old answer, but isn't this an example of the binary plus rather than the unary plus? Nov 2, 2018 at 15:54
  • 2
    @AuroraVollvik: It's unary plus if the macro happens to be defined but with empty definition. Nov 2, 2018 at 16:15
  • 2
    @UndefinedBehavior: Whether it's unary or binary depends on how FOO is defined. Feb 7, 2019 at 19:02
19

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
1
  • 1
    Finally. A potentially useful application that will inform some generic code of the size of an argument following integer promotion that may not be sizeof(int). I've got no idea when I'll use it. But nice all the same. NB: edited answer to show that not everything goes to 4.
    – Persixty
    Sep 7, 2017 at 11:08
18

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

int arr[] = {
    +4,
    -1,
    +1,
    -4,
};
1
  • 2
    No way this is cleaner than the alternative without the unary +s.
    – ruohola
    Jun 13, 2019 at 12:26
15

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
1
  • 2
    This is useful for printing chars in C++ with std::cout.
    – S.S. Anne
    Jan 13, 2020 at 22:31
14

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.

1
1

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.

2
  • 2
    By 'noop', I meant 'doing nothing'–I expect compilers to not add an instruction for the sake of adding an instruction.
    – zneak
    Jul 10, 2011 at 18:01
  • 3
    But +(char)4 is not the same as (char)4. Jul 11, 2011 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.