A girl and a robot thinking together

Think together · Start with a story

A toy counter has only two digits. After 99, it cannot show 100. Integer types in computers also have limits.

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Pick a typeIt has a limited range
  2. CalculateAdd or multiply values
  3. Check the resultDid it go beyond the range?

Integer types can represent only a limited range. Inputs may fit even when their sum or product does not.

01Why can a number “not fit”?

Each integer type can represent only a specific range of values. A result outside that range cannot be represented correctly by that type.

02How large is a 32-bit int?

A common 32-bit signed integer range is:

-2,147,483,648to2,147,483,647

The exact range depends on the type and implementation. Do not assume all integer types can store every whole number.

03What happens with signed integer overflow?

In C++, signed integer overflow is undefined behavior.

This means the language does not promise a particular result. A program must not rely on the value automatically wrapping around.

04Why are unsigned integers different?

For an N-bit unsigned integer, arithmetic is defined modulo 2^N.

unsigned int x = UINT_MAX;
x = x + 1;   // wraps around to 0

This wraparound is behavior defined by the language.

05How can you reduce overflow risk?

Choose a suitable type

Use a wider integer type when you need a larger range.

Estimate the range first

Work out how large the inputs and intermediate values can get.

Check before calculating

When needed, check whether the result may go out of range.

Watch intermediate results

The final result may fit even if an earlier calculation overflows.

06Why is long long often useful?

In programming contests, long long is often used when a larger integer range is needed.

The C++ standard guarantees at least 64 bits of value width for long long; common modern implementations usually use 64 bits.

long long total = 1000000000LL * 1000000000LL;

The LL suffix makes the literal take part in wider integer arithmetic, avoiding a calculation in a smaller type first.

07Why are intermediate results easy to overlook?

int a = 100000;
int b = 100000;
long long c = a * b;

Even though c is a long long, the expression a * b may be evaluated as an int first and overflow.

One safer way to write it is:

long long c = 1LL * a * b;

What you have learned

  • Integer types can represent only a limited range.
  • Signed integer overflow is undefined behavior in C++.
  • Unsigned integers wrap around modulo 2^N.
  • A wider type can expand the range, but the types of intermediate expressions matter too.
  • long long is common in programming contests, but you still need to estimate ranges.

Topic complete: Next, you can explore “Program Flow.”

Your turn to try

If you multiply using int and store the result in long long, is it guaranteed to be safe?

Ready? Open to see the explanation

Not necessarily. The multiplication may already have overflowed as an int. The operation must use a sufficiently wide type, not just a larger final container.