DEV Community

Absolute Number in C Language

Sukma Wardana on January 30, 2024

An absolute value in mathematics is defined as the non-negative value of x without regard to its sign. The notation of absolute value is a vertical...
Collapse
 
pauljlucas profile image
Paul J. Lucas

A much simpler implementation is:

int absolute( int x ) {
   return x < 0 ? -x : x;
}
Enter fullscreen mode Exit fullscreen mode

And C has had a boolean type since C99; see here.

Collapse
 
sukma_wardana profile image
Sukma Wardana

Oh, you're right? That's simpler and easier to understand. Thanks for sharing the solutions and info about boolean type in C. I will add it to my notes :)