Discussion
The statement
printf( " % f ", ( float )9/5) ;
prints
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development