The modulo operator % is the one arithmetic operator that trips up beginners most — because it doesn’t divide, it reports what’s left over after dividing. This question from our C Programming Quiz App (it appears twice in the question bank, as #8 and #137) checks whether you can tell the remainder from the quotient.
The Quiz Question
printf("%d", 10 % 3);
What is printed by this code?
- 1
- 3
- 0
- 3.33
The Correct Answer: 1
10 divided by 3 is 3 with remainder 1 — and % yields the remainder. Verified on gcc 13.3 and Apple clang 21, clean under -Wall -Wextra:
$ gcc -Wall -Wextra mod.c && ./a.out 1
The identity worth memorizing: for integers, a == (a / b) * b + (a % b). With 10 and 3: 3 * 3 + 1 = 10. Division and modulo are two halves of the same operation — / keeps the quotient, % keeps the rest.
Why Each Wrong Answer Is Wrong
Why not 3?
3 is the quotient — what 10 / 3 yields under integer division. We ran both side by side:
printf("%d\n", 10 % 3); /* 1 — remainder */
printf("%d\n", 10 / 3); /* 3 — quotient */
Mixing these up is the classic error. / answers “how many times does it fit?”; % answers “what’s left?”.
Why not 0?
The remainder is 0 only when the division is exact — 9 % 3 or 12 % 3. That’s precisely why x % 2 == 0 is the standard even-number test: an even number leaves nothing behind when divided by 2. 10 is not a multiple of 3, so its remainder is nonzero.
Why not 3.33?
3.33 is what floating-point division would give — but there are no floats here, and % wouldn’t accept them anyway. It’s defined only for integers; feeding it doubles is a hard compile error on both compilers:
$ gcc mod_float.c
mod_float.c:3:25: error: invalid operands to binary % (have 'double' and 'double')
$ clang mod_float.c
mod_float.c:3:25: error: invalid operands to binary expression ('double' and 'double')
For floating-point remainders, the standard library provides fmod() from <math.h> — fmod(10.0, 3.0) returns 1.000000, matching the integer result.
Negative Numbers: The Part Interviews Ask
Since C99, integer division truncates toward zero, which pins down the sign of %: the result takes the sign of the dividend (the left operand). We verified:
printf("%d\n", -10 % 3); /* -1, not 2 */
Languages like Python return 2 here (sign of the divisor), so programmers switching languages get bitten. If you need a always-non-negative result in C, use ((a % b) + b) % b.
Where % earns its keep: even/odd tests (n % 2), wrapping array indices ((i + 1) % size for circular buffers), extracting digits (n % 10), and clock arithmetic ((hour + 5) % 24).
Frequently Asked Questions
What does 10 % 3 return in C?
1 — the remainder after integer division. 10 / 3 fits 3 times (quotient 3), leaving 10 − 9 = 1.
Does the modulo operator work on floats in C?
No — % requires integer operands; 10.0 % 3.0 is a compile error on both gcc and clang. Use fmod() from <math.h> for floating-point remainders.
What is -10 % 3 in C?
-1. Since C99, the result of % takes the sign of the left operand (the dividend), because division truncates toward zero. This differs from Python, where -10 % 3 is 2.
Related Reading
- Arithmetic Operators in C
- Equality Operator in C – Why 3 == 3 Prints 1
- Short-Circuit Evaluation in C
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question appears as #8 and #137 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →