

abs()                    General Function                   abs()




Return the absolute value of an integer

iinntt aabbss(_n) iinntt _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>=0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  aabbss of the  most negative in-
teger is itself.







































COHERENT Lexicon                                           Page 2


