What do arithmetic underflow and overflow mean in C programming?
4 Answers
Overflow
From http://en.wikipedia.org/wiki/Arithmetic_overflow:
the condition that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.
So, for instance:
uint32_t x = 1UL << 31;
x *= 2; // Overflow!
Note that as @R mentions in a comment below, the C standard suggests:
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
Of course, this is a fairly idiosyncratic definition of "overflow". Most people would refer to modulo reduction (i.e wrap-around) as "overflow".
Underflow
From http://en.wikipedia.org/wiki/Arithmetic_underflow:
the condition in a computer program that can occur when the true result of a floating point operation is smaller in magnitude (that is, closer to zero) than the smallest value representable as a normal floating point number in the target datatype.
So, for instance:
float x = 1e-30;
x /= 1e20; // Underflow!

- 267,707
- 33
- 569
- 680
-
6Your example of overflow is wrong in the language of the C standard. Unsigned types *cannot overflow*. – R.. GitHub STOP HELPING ICE Jun 16 '11 at 02:15
-
2@R.: "Overflow" certainly has a well-understood meaning independent of the C standard! But you're right, so I'll include that factoid in my answer. – Oliver Charlesworth Jun 16 '11 at 07:18
-
The distinction is mainly important when people say things like "integer overflow always results in undefined behavior". – R.. GitHub STOP HELPING ICE Jun 16 '11 at 11:26
-
What about `unsigned char x = 0; x -= 1;` ? is this count as an underflow? – Gamal Othman Feb 25 '19 at 14:48
-
1I found on [Wikipedia](https://en.wikipedia.org/wiki/Arithmetic_underflow) that it is called "Integer Overflow" or "Integer Wraparound". It says: _Note that storing values that are too low in an integer variable (e.g. attempting to store -1 in an unsigned integer) is properly referred to as integer overflow, or more broadly "integer wraparound". The term underflow normally refers to floating point numbers only, and is a separate issue._ Thanks! – Gamal Othman Feb 25 '19 at 14:58
Computers use only 0 and 1 to represent data so that the range of values that can be represented is limited. Many computers use 32 bits to store integers, so the largest unsigned integer that can be stored in this case is 2^32 -1 = 4294967295. But the first bit is used to represent the sign, so, in fact, the largest value is 2^31 - 1 = 2147483647.
The situation where an integer outside the allowed range requires more bits than can be stored is called an overflow.
Similarly, with real numbers, an exponent that is too small to be stored causes an underflow.

- 579
- 3
- 7
- 15
int, the most common data type in C, is a 32-bit data type. This means that each int is given 32 bits in memory. If I had the variable
int a = 2;
that would actually be represented in memory as a 32-bit binary number: 00000000000000000000000000000010.
If you have two binary numbers such as
10000000000000000000000000000000
and
10000000000000000000000000000000,
their sum would be 100000000000000000000000000000000, which is 33 bits long. However, the computer only takes the 32 least significant bits, which are all 0. In this case the computer recognizes that the sum is greater than what can be stored in 32 bits, and gives an overflow error.
An underflow is basically the same thing happening in the opposite direction. The floating-point standard used for C allows for 23 bits after the decimal place; if the number has precision beyond this point it won't be able to store those bits. This results in an underflow error and/or loss of precision.

- 202
- 2
- 12
-
A computer normally has no checks that an operation resulted in overflow. And where did you get 23 from? – Oliver Charlesworth Jun 16 '11 at 00:37
-
1
-
@OliCharlesworth: Under IEEE 754 a single-precision floating-point number has a 23 _bit_ (not digit) significand. There's also an implicit bit which normally gives you 24 bits of precision but that doesn't apply during underflow, which uses subnormal numbers to provide a gradual loss of precision. – Michael Carman Jun 21 '13 at 14:35
-
Your description of underflow is completely wrong. Floating-point numbers consist of three fields: sign, significand (fraction), and exponent. The number of bits in the significand determine the precision (roughly 7 decimal characters for a 32-bit float). The number of bits in the exponent determine the range (smallest to largest) of representable values. The location of the decimal point is determined by the value of the exponent and independent of precision. Underflow occurs when the value to be stored is smaller in magnitude than the minimum value that the exponent can support. – Michael Carman Jun 21 '13 at 14:57
-
@Oli Charlesworth: normally every CPU has an overflow flag in its status register. (Maybe you meant that C doesn't care about this) – Curd Jan 13 '14 at 13:47
-
@Curd: Yeah, I can't remember what I meant at the time! Hopefully I meant that you couldn't access the status flag from C... – Oliver Charlesworth Jan 13 '14 at 13:50
-
underflow depends exclusively upon the given algorithm and the given input data,and hence there is no direct control by the programmer .Overflow on the other hand, depends upon the arbitrary choice of the programmer for the amount of memory space reserved for each stack ,and this choice does influence the number of times overflow may occur

- 1
-
1you are mixing things: arithmetic underflow with stack overflow and putting them together, making your answer confusing at best. I am not sure I agree with you definitions either. – bolov Oct 26 '14 at 15:03
-
Your answer say only what's the difference between them, but doesn't say **what** they are; the question's title is "_what is underflow and over flow_" – GingerPlusPlus Oct 26 '14 at 15:04