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

zneak picture zneak · Jul 9, 2011 · Viewed 21.5k times · Source

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?

Answer

Nemo picture Nemo · Jul 9, 2011

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.