Practice mode C Language

What is printed by this C code: printf(“%d %d”, -7 / 2, -7 % 2);?

Correct answer D. -3 -1

Explanation

Since C99, integer division truncates towards zero, so -7 / 2 is -3, not -4. The remainder is defined so that (a / b) * b + a % b equals a: (-3) * 2 + r = -7 gives r = -1. So the result of % takes the sign of the dividend. The pair -4 1 would follow floor division, which C does not use, although languages such as Python do.

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.