# Why do C and C++ allow the expression (int) + 4*5?

``````(int) + 4*5;
``````

Why is this (adding a type with a value) possible? (tried with g++ and gcc.)

I know that it doesn't make sense (and has no effect), but I want to know why this is possible.

• same as `(int)-4*5` – P__J__ Apr 21 at 15:29
• There is a useful tool called `cppinsights` that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about your example (the same 'parenthesization' as the answers your were given) – Nikita Kniazev Apr 21 at 21:02
• This statement is equivalent to `+(int)+ 4*5;` and `-(int)- 4*5;` and `-+-+-(int)-+-+- 4*5;` and less poetically `;` – chqrlie Apr 22 at 0:12
• What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign. – Carsten S Apr 22 at 6:26
• Shouldn't C++ warn you that C-type casts are not recommended in C++? – Mr Lister Apr 22 at 9:38

The `+` here is unary `+` operator, not the binary addition operator. There's no addition happening here.

Also, the syntax `(int)` is used for typecasting.

You can re-read that statement as

``````(int) (+ 4) * 5;
``````

which is parsed as

``````((int) (+ 4)) * (5);
``````

which says,

1. Apply the unary `+` operator on the integer constant value `4`.
2. typecast to an `int`
3. multiply with operand `5`

This is similar to `(int) (- 4) * (5);`, where the usage of the unary operator is more familiar.

In your case, the unary `+` and the cast to `int` - both are redundant.

• "Casting", not "typecasting". Typecasting is something that happens to actors. – Keith Thompson Apr 21 at 23:41
• `(+ 4)` is not make the operand `+4`, it means apply the unary `+` to operand `4`, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example `char c = 0; sizeof +c == sizeof c` is probably false and `sizeof +"a"` is probably not 2. – chqrlie Apr 22 at 0:07
• "both are redundant" - the whole thing is redundant, just like `42;` :-) – paxdiablo Apr 22 at 8:17
• I see nothing wrong with using the term Type Casting. It seems I'm not the only one. – Ben Apr 22 at 15:28
• @Ben Typecasting is not type casting. – Kenneth K. Apr 22 at 17:05

This is interpreted as `((int)(+4)) * 5`. That is, an expression `+4` (a unary plus operator applied to a literal `4`), cast to type `int` with a C-style cast, and the result multiplied by `5`.