LOGBOOK

HELP

Quiz Entry - updated: 2026.07.07

Your C program multiplies positive numbers but suddenly produces negative results. What's happening and how do you diagnose it?

Integer overflow: the result passed the type's maximum and wrapped around into the negative range.

The fix usually starts with the types — an int tops out near 2.1 billion, so a calculation that should grow past that silently wraps. Widen the type (or check the operands) before the operation, and watch for intermediate results overflowing even when the final answer would fit.

Diagnosis steps:

  1. Check the data types involved (int max is ~2.1 billion)
  2. Add debug output before the operation to see values
  3. Consider if intermediate results overflow, not just final

Solutions:

  • Use larger types (long long instead of int)
  • Check for overflow before operations
  • Use compiler flags like -ftrapv (GCC) to catch overflows

Warning signs:

  • Positive × Positive = Negative
  • Large positive + Large positive = Negative
  • Calculations that "should" grow suddenly shrink

Go deeper:

From Quiz: REVE1 / Overview of Computer Systems | Updated: Jul 07, 2026