This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Overflow handling in C

Hello to all.
Please excuse my naive question, but to me it's a real problem.
I want to add to signed nummber's.
If an overflow occurs the sign is changed unwanted!
Don't like it, better the result is limited and the sign doesn't change
Here is my solution:

 signed long int a,b,c;

//some AD-Converting stuff

  a = b + c;
  if (a<0 && b>0 && c>0) a=0x7FFFFFFF;
  if (a>=0 && b<0 && c<0) a=0x80000000;


Maybe this will work.
But can i be sure that it will work under any circumstances?
Is it a good solution?
Or will it consume lot of recources? (cause there in the IF statements is a lot of comparation to do)

How can i manage this similar case when i want to add a nummber a to b:

 signed long int a,b;

//some AD-Converting stuff

  a += b;

Ones again i want to excause myself i'am no native english speaker and i have only very little experience in programming C :-)

0