Answer E10

Expression evaluates to: 12.000000

This answer might surprise you. Here is the statement:

  printf("Expression evaluates to: %f\n", 10.0 + 5/2 );  /* division is done first */
  1. Division has higher precedence than addition, so 5/2 is done first.
  2. 5/2 produces integer 2
  3. Now addition is performed: double + int
  4. The int 2 is promoted to double 2.0
  5. 10.0 + 2.0 results in double 12.0
  6. The value of the entire expression is 12.0, a double

It is a mistake to think that the 10.0 makes the entire expression floating point. Expressions are evaluated subexpression by subexpression. If both operands of a subexpression are int, then the operation and value of the subexpression are int. If the next operation included a double the subexpression then is promoted to a double.



Back to Puzzle Home