This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
abs
Prototype
#include <stdlib.h>
int abs(int x);
Description
This function returns the absolute value of the argument integer x
.
If the argument value is positive, abs
returns it
unmodified, otherwise if the value is negative, abs
returns it with
the positive sign.
Implementation
#include <stdlib.h>
int abs(int x)
{
return (x < 0) ? -x : x;
}
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("abs(%d) -> %d\n", 0, abs(0));
printf("abs(%d) -> %d\n", 1, abs(1));
printf("abs(%d) -> %d\n", -1, abs(-1));
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 7.10.6.1