|
|
coherent
abs() General Function abs()
Return the absolute value of an integer
iinntt aabbss(_n) iinntt _n;
abs returns the absolute value of integer _n. The _a_b_s_o_l_u_t_e _v_a_l_u_e
of a number is its distance from zero. This is _n if _n>=00, 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 aabbss of the most negative in-
teger is itself.
COHERENT Lexicon Page 2
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.