ABS for longs and ints

Alan J Rosenthal flaps at dgp.toronto.edu
Sun Oct 1 04:03:37 AEST 1989


gmaranca at cipc1.Dayton.NCR.COM (Gabriel Maranca) writes:
>#define ABS(x) (((long)(x) < 0L)? -(x) : (x))
...
>Is this non-ANSI or unportable? It works for me.

It is portable but will only work for ints and longs.  But the following
is portable and will work for all types, including unsigned long, double,
and long long if it exists:

#define ABS(x) (((x) < 0) ? -(x) : (x))

The zero will get promoted to the correct type.

Like hey folks, C fully supports "mixed mode" arithmetic.  It isn't fortran.

ajr

--
Vs encr vf n xvaq bs frk, gura n chapu va gur zbhgu vf n xvaq bs gnyxvat.



More information about the Comp.lang.c mailing list