C Programming · Interview question

What's the difference between signed and unsigned integer overflow?

A strong answer

Unsigned overflow is fully defined: it wraps modulo 2^N, so UINT_MAX + 1 == 0 is guaranteed and portable, that's why ring-buffer indices, hash functions, and checksums use unsigned types and deliberately rely on wraparound. Signed overflow, by contrast, is undefined behavior: INT_MAX + 1 has no defined result, and the compiler may assume it never occurs. That assumption powers optimizations like turning x + 1 > x into true or simplifying loop bounds, which means code that "relies on" signed wraparound can break at higher optimization levels even though it appears to work. Practical rules: use unsigned types whenever you need wraparound semantics, be careful that intermediate signed arithmetic can't overflow, and use -fsanitize=undefined to catch signed overflow at runtime during testing.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Undefined Behavior & Pitfalls

Why code that works at -O0 breaks at -O2: what undefined behavior is, the catalog of UB every firmware engineer must recognize, and how sanitizers catch it.

More Undefined Behavior & Pitfalls questions

Browse all 472 interview questions
What's the difference between signed and unsigned integer overflow? | EmbeddedPrep.io