Tutorial :How to ignore the sign of a variable in C?



Question:

I am using C as the programming language. How to ignore the sign when using float/signed int variables? For example if float/signed int f = -40 or +40 The result should be same after a mathematical operation like a+b*f


Solution:1

Use abs for ints or fabs for floats.

a+b*abs(f)  

EDIT: It's not clear wether you want -40 to be treated as 40 or vice versa, if you for some reason wan't the latter:

a+b*-abs(f)  


Solution:2

Are you looking for absolute value?

#include<math.h> includes abs for integers, fabs for floats.


Solution:3

abs or fabs?


Solution:4

use the function abs() to return the absolute value


Solution:5

fabsf(f) returns the absolute value of f: fabsf(40) == 40 and also fabsf(-40) == 40.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »