# detect variable overflow

• 05-12-2004
surdy
detect variable overflow
is there a provision by which i can detect overflow in a variable during arithmatic operation
for example i use an unsigned char variable a to store the sum of b and c both unsigned chars. I want to detect if b+c is more that 255 (i.e more than what 8-bit number can store).

• 05-12-2004
Dave_Sinkula
Here are a couple of ways.
Code:

```#include <stdio.h> #include <limits.h> void foo(unsigned char b, unsigned char c) {   unsigned char a = b + c;   if ( a < c )   {       printf("overflow occurred, ");   }   printf("a = %d\n", a); } void bar(unsigned char b, unsigned char c) {   unsigned char a = b + c;   if ( b > UCHAR_MAX - c )   {       printf("overflow occurred, ");   }   printf("a = %d\n", a); } int main(void) {   foo(10, 10);   foo(UCHAR_MAX - 5, 10);   foo(10, UCHAR_MAX - 5);   foo(UCHAR_MAX - 5, UCHAR_MAX - 5);   bar(10, 10);   bar(UCHAR_MAX - 5, 10);   bar(10, UCHAR_MAX - 5);   bar(UCHAR_MAX - 5, UCHAR_MAX - 5);   return 0; } /* my output a = 20 overflow occurred, a = 4 overflow occurred, a = 4 overflow occurred, a = 244 a = 20 overflow occurred, a = 4 overflow occurred, a = 4 overflow occurred, a = 244 */```
• 05-12-2004
quzah
Code:

```int overflow( unsigned char a, unsigned char b ) {         return UCHAR_MAX - a < b; /*Good point Dave.[/edit]*/         /*return  a > b ? UCHAR_MAX - a < b : UCHAR_MAX - b < a;*/ }```
Something like that will suffice. You can do the same with other data types using the same concept. This will actually avoid the overflow, rather than relying on the overflow itself to actually happen to see if it comes out the way you expect.

For some reason I was thinking the order mattered. Thank's Dave. I need a nap.[/edit]

Quzah.
• 05-12-2004
anonytmouse
..Beaten..

>> for example i use an unsigned char variable a to store the sum of b and c both unsigned chars ... I want to detect if b+c is more that 255 <<

If a and b are both unsigned characters you can do it in the normal way:
Code:

`if (a + b > 255)`
This is because C promotes types smaller than int to int before performing arithmetic operations on them. So in the above expression a and b are treated as ints and no overflow will occur.

You can get into trouble when you wish to add two ints together as these can overflow.

You must add an extra overflow check in this scenario.
Code:

```if (A + B >= A && A + B < MAX) {   // No overflow and less than MAX } // Or, the other way around. if (A + B < A) {   printf("OVERFLOW: Stop trying to hack me!"); }```
Here is a good article on integer overflows.
• 05-13-2004
surdy
thanx quzah ....thanx anonytmouse
but my problem still remains unsolved
i was looking for something inbuilt in c .....some provision given by c..like some flag that is set or something like this
and why i am looking for that is because of following:
my problem is not as simple as adding just 2 unsigned chars ..........suppose i have to add three unsigned chars a,b,c i used "if(a+b+c<a)" (similar to used in the foo function) before posting my query on the board now consider a=23 b=255 c=1 so now a+b+c=a therefore overflow not detected
i can handle this special case (actally i have done) as an excetional case but was looking for a better solution
any suggestions???
• 05-13-2004
Kip Neuhart
>i was looking for something inbuilt in c .....some provision given by c..like some flag that is set or something like this
The spirit of C is that you as the programmer know exactly what you're doing. The language definition will not protect you from yourself in cases such as these, so you must do it manually.

>any suggestions???
Write a function similar to quzah's with a variable number of arguments that tests and calculates intermediate results incrementally. After "running the gauntlet", you can return the result, or flag an error if any of the tests fail.
• 05-13-2004
surdy
Quote:

Originally Posted by Kip Neuhart
>i was looking for something inbuilt in c .....some provision given by c..like some flag that is set or something like this
The spirit of C is that you as the programmer know exactly what you're doing.

i respect that and i am the beliver of doing things myself
as i told u i have sucessfully handled the situation so that's not an issue
all i wanted to know that is there some thing in c like the carry flags in microprocessors that may be set if there is an overflow in some variable
that's it
• 05-13-2004
Kip Neuhart
Quote:

all i wanted to know that is there some thing in c like the carry flags in microprocessors that may be set if there is an overflow in some variable
that's it
• 05-13-2004
surdy
I'm very sorry for not crediting(by mistake) the greatest contributer DAVE_sinkula

thanx dave
though my question is still unaswered
• 05-13-2004
surdy
Quote:

Originally Posted by Kip Neuhart