Machine-translated by Google. Questions are authored in English.
Top

Discussion

In C programming language, if the first and the second operands of operator + are of types int and float, respectively, the result will be of type

  • A.int
  • B.float
  • C.char
  • D.long int

Answer: B

Correct answer: float

The usual arithmetic conversions apply: when the operands of a binary operator have different types, the one of lower rank is converted to the type of the other before the operation, and the result has that common type.

Here int is converted to float, so the addition is performed in floating point and the result is float. The rule is why integer division is such a common source of surprise - 1/2 is 0 because both operands are int, whereas 1/2.0 is 0.5.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.