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

Discussion

The statement 

printf( " % f ", ( float )9/5) ;

prints

  • A.1.8
  • B.1.0
  • C.2.0
  • D.none of these

Answer: A

Correct answer: 1.8

A cast binds more tightly than division, so the expression is ((float)9) / 5. The left operand is now floating point, so the usual arithmetic conversions promote 5 to floating point too and the division is performed in floating point:

\[ 9.0 / 5 = 1.8 \]

Without the cast, 9/5 would be integer division giving 1, and printing that with %f would be undefined behaviour rather than merely wrong. Casting one operand is the standard way to force a floating point division.

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

Post your comment

Please log in or register to post a comment.