Possible Duplicate:
Best way to detect integer overflow in C/C++
I am writing a function in C but the question is generic. The function takes three integers and returns some information about these three integers.
Problem I suspect here is the integers can be at their max and this can cause overflow.
For example: if I pass a as maximum value possible and b can be anything 1 - max, then in this case, will the expression (a+b)>c in if condition cause overflow? If so, how do I handle it?
My solution was to keep a long integer as temporary variable to keep value of a+b and use it in the expression but it sounds dirty way.
Refer to this snippet:
int
triangle_type(int a, int b, int c) {
if (!((a+b)>c)&&((b+c) > a)&&((a+c>b))) {
return -1;
}
}