Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language overflow judgment
C language overflow judgment
This is a series accumulated from 2 147483644 in the console program written with VC6. You can see that there is no error prompt when overflowing:

2 147483644

2 147483645

2 147483646

2 147483647

-2 147483648

-2 147483647

-2 147483646

-2 147483645

-2 147483644

-2 147483643

-2 147483642

This is the addition function I wrote to judge overflow:

# include & ltstdio.h & gt

int uoadd (int a,int b,int * overflow);

Int overflow;

int main()

{

int a=2 147483647-9,b= 10,c = 0;

C=uoadd(a, b, & overflow);

If (overflow == 1)

Printf ("Overflow! \ n ");

other

printf ("%d\n ",c);

return(0);

}

int uoadd (int a,int b,int *overflow)

{

* Overflow = 0;

If (A>0 & AMPB & GT0 & AMPA+B <; 0)* overflow =1;

If (A<0 & AMPB & LT0 & AMPA+B > 0)* overflow =1;

Return (a+b);

}

VC6 compilation, select the console program when building a new project.

In fact, other applications may have something built in to judge overflow. For example, VB will judge. If the data overflows, a dialog box will pop up, and the rest is unknown.

In fact, it is quite easy to solve this problem with assembly, but considering readability, it is better to use C.