abs() General Function abs()
Return the absolute value of an integer
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>
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(1);
}
if (!isdigit(input))
if (input != '-' || counter != 0) {
fprintf(stderr,
"%s is not a number\n", string);
exit(1);
}
COHERENT Lexicon Page 1
abs() General Function abs()
}
input = atoi(string);
printf("abs(%d) is %d\n", input, abs(input));
exit(0);
}
***** See Also *****
fabs(), floor(), general functions, int
***** Notes *****
On two's complement machines, the abs of the most negative in-
teger is itself.
COHERENT Lexicon Page 2