COHERENT manpages
This page displays the COHERENT manpage for abs() [Return the absolute value of an integer].
List of available manpages
Index
abs() -- General Function (libc)
Return the absolute value of an integer
#include <stdlib.h>
int abs(n) int n;
abs() returns the absolute value of integer n. The absolute value of a
number is its distance from zero. This is n if n>=0, and -n otherwise.
Example
This example prompts for a number, and returns its absolute value.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
extern char *gets();
extern int atoi();
char string[64];
int counter;
int input;
printf("Enter an integer: ");
fflush(stdout);
gets(string);
for (counter=0; counter < strlen(string); counter++) {
input = string[counter];
if (!isascii(input)) {
fprintf(stderr,
"%s is not ASCII\n", string);
exit(EXIT_FAILURE);
}
if (!isdigit(input))
if (input != '-' || counter != 0) {
fprintf(stderr,
"%s is not a number\n", string);
exit(1);
}
}
input = atoi(string);
printf("abs(%d) is %d\n", input, abs(input));
exit(EXIT_SUCCESS);
}
See Also
fabs(),
floor(),
int,
libc,
stdlib.h
ANSI Standard, §7.10.6.1
POSIX Standard, §8.1
Notes
On two's complement machines, the abs() of the most negative integer is
itself.

















