Practice mode C Language

What is printed by this C code: printf(“%d”, (int)-3.99);?

Correct answer B. -3

Explanation

Converting a floating value to an integer type discards the fractional part, which means truncation towards zero rather than rounding. So -3.99 becomes -3, not -4. The value -4 would come from floor(-3.99) and the result is not -3.99 because the cast produces an int. If the truncated value does not fit in the target type the behaviour is undefined, but -3 fits easily.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.