# 3.7.1 – Formulating overflow and underflow

by on September 6, 2017

### Formulating overflow and underflow (Integer Types):

In previous sessions, we have discoursed what actually happens on overflow and underflow of integer types. It is difficult to guess the result once the data overflows or underflow. Now we will derive a couple of formulas to find the result on overflow and underflow

As we have discoursed so far, overflow results cycling towards negative side and underflow results cycling towards positive side. We have also discoursed why is it so? Now, we will summarize what would be the result if overflows and what would be the result if underflow.

### Resultant value if overflows:

The formula to find the resultant value on overflow is

TYPE_MIN_LIMIT+(CURRENT_VALUE – TYPE_MAX_LIMIT) – 1

Let us apply the formula to some practical cases. Say for example, if the limit of short type is -32768 to +32767 then assigning 32770 would result overflow and the output would be printed as -32766 [-32768 + (32770-32767) – 1]

Assigning 32785 would result overflow and the output would be printed as -32751 [-32768 + (32785-32767) – 1]

```#include <stdio.h>
int main()
{
short int x=32770;
short int y=32785;
printf("%d",x);
printf("\n%d",y);
return 0;
}
```

Output:
-32766
-32751

Now let us apply the same formula to unsigned types. Say for example, if the limit of unsigned short is 0 to 65535 then assigning 65540 would result overflow and the output would be printed as 4 [0 + (65540 – 65535) – 1]

```#include <stdio.h>
int main()
{
unsigned short x=65540;
printf("%u",x);
return 0;
}
```

Output:
4

Let us apply the same with the character type. As the range of char type is -128 to +127, assigning 200 results overflow and the result would be -56 [-128 – (200-127) -1] and equivalent character to -56 or 200 is a graphical character ╚

```#include <stdio.h>
int main()
{
char ch=200;
printf("%d",ch);
printf("\n%c",ch);
return 0;
}
```

Output:
-56

We can also apply the same with the unsigned char type. As the range of unsigned type is 0 to 255, assigning 300 would result -44 [0 – (300-255) -1] and equivalent character to 44 is ,

```#include <stdio.h>
int main()
{
unsigned char ch=300;
printf("%d",ch);
printf("\n%c",ch);
return 0;
}
```

Output:
44
,

### Resultant value if underflow:

The formula to find the resultant value on underflow is

TYPE_MAX_LIMIT – (CURRENT_VALUE – TYPE_MIN_LIMIT) +1

Let us apply the formula for some practical cases. Say for example, if the limit of short type is -32768 to +32767 then assigning -32772 would result underflow and the output would be printed as 32764 [32767 – (-32772 – -32768) + 1]

Assigning -32790 would result underflow and the output would be printed as 32746 [32767 – (-32790 – -32768)+1]

```#include <stdio.h>
int main()
{
short int x=-32772;
short int y=-32790;
printf("%d",x);
printf("\n%d",y);
return 0;
}
```

Output:
32764
32746

It also applies to the unsigned types. Say for example, if the limit of unsigned short is 0 to 65535 then assigning -10 would result underflow and the output would be printed as 65526 [ 65535 – (-10 – 0 ) +1]

```#include <stdio.h>
int main()
{
unsigned short x=-10;
printf("%u",x);
return 0;
}
```

Output:
65526

Let us check the formula with the char type. Say for example, if the limit of char type is -128 to 127 then assigning -200 would result underflow and the output would be printed as 56 [127 – (-200 – -128)+1] and equivalent character to 56 is ‘8’.

```#include<stdio.h>
int main()
{
char ch=-200;
printf("%d",ch);
printf("\n%c",ch);
return 0;
}
```

Output:
56
8

Let us check the formula with the unsigned char type. Say for example, if the limit of unsigned char is 0 to 255 then assigning -100 would result underflow and the output would be printed as 156 [255 – (-100 – 0)+1] and its equivalent character would be £

```#include<stdio.h>
int main()
{
unsigned char ch=-100;
printf("%d",ch);
printf("\n%c",ch);
return 0;
}
```

Output:
156
£

What would be the output of following programs:

```#include<stdio.h>
int main()
{
signed short x,a;
unsigned short y,b;
a=(signed short)10u;
b=(unsigned short)-10;
y = (signed short)10u + (unsigned short)-10;
x=y;
printf("a=%d\nb=%u",a,b);
printf("\nx=%d\ny=%u",x,y);
return 0;
}
```

Output:
a=10
b=65526
x=0
y=0

Example explained:

```a=(signed short)10u;
```

Here 10u is within the range of signed short. Hence, it is properly assigned to the variable a

```b=(unsigned short)-10;
```

As the range of unsigned short is 0 to 65535, assigning -10 results underflow and 65526 [65535 – (-10 – 0) + 1] would be assigned to the variable b

``` y = (signed short)10u + (unsigned short)-10;
```

Here 10+65526 is 65536, assigning which to an unsigned short results overflow and 0 [0 + (65536-65535) -1] would be assigned to the variable y.

```x=y;
```

Now, the value of y is 0, which is in the range of signed short. Hence, this is properly assigned to the variable x

Previous post:

Next post: