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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)