• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

question about subtracting 2 values in C

BALIstik916

Senior member
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?
 
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?
 
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.
 
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.
 
Back
Top