Hi.
I dont know how to efficiently check if a given type range has been surpassed, e.g.
int (32 bit)
range 0 - 4294967295
having:
int number =4294967293
int sum = number + 4355435;
and now sum overflowed...
Please help.
Hi.
I dont know how to efficiently check if a given type range has been surpassed, e.g.
int (32 bit)
range 0 - 4294967295
having:
int number =4294967293
int sum = number + 4355435;
and now sum overflowed...
Please help.
Unsigned int (32 bit) has range [0, 4294967296]. But if defined as you do, it's signed: range [-2147483648, 2147483647]. You should either do extra work yourself to ascertain overflow doesn't occur, or use a more suitable datatype (like 64 bits long long).
Code:#include <stdio.h> #include <limits.h> int main(void) { long a = LONG_MAX - 2, b = 5; if ( b > LONG_MAX - a ) { puts("overflow will occur for a + b"); } return 0; } /* my output overflow will occur for a + b */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*