question about subtracting 2 values in C

BALIstik916

Senior member
Jan 28, 2007
755
0
71
if i were to find the difference between 2 values of time (in military form) such as 1200 and 1400 and im using the variable type long and then using the absolute value function

long timediff = labs(timeout - timein);

it always returns the difference as 255, can anyone help me out on this?
 

sao123

Lifer
May 27, 2002
12,656
207
106
it might be helpful if you post some actual code.

255, is the absolute value of an signed 8 bit space. it sounds like you have a type mismatch somewhere. What type is timein and timeout declared as?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,835
4,815
75
And/or just do:

long timediff = labs((long)timeout - (long)timein);

But...if your values are in that format, won't 1400-1359 = 41?

And finally, why long? 2359 easily fits in an int.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: sao123
it might be helpful if you post some actual code.
Agreed.
Show us how timeout and timein are declared and initialized.

255, is the absolute value of an signed 8 bit space. ...

255 is the absolute value of an unsigned 8 bit space. ;-)

Originally posted by: Ken g6
And/or just do:

long timediff = labs((long)timeout - (long)timein);

But...if your values are in that format, won't 1400-1359 = 41?
Pay attention to this, OP. Its fairly common in C to represent times as 'seconds elapsed since the epoch' -- its not clear if the OP is doing this, however, as we don't see all of the code.