Name
abs
Synopsis
Gives the absolute value of an integer
#include <stdlib.h> intabs
( intn
); longlabs
( longn
); long longllabs
( long longn
);
The abs()
functions return
the absolute value of the integer argument
n
; if n
is
greater than or equal to 0, the return value is equal to
n
. If n
is
less than 0, the function returns - n
.
Example
int amount = -1234;
char currencysym[2] = "$";
char sign[2] = "-";
div_t dollarsandcents = { 0, 0 };
if ( amount >= 0 )
sign[0] = '\0';
dollarsandcents = div(abs
( amount ), 100 );
printf( "The balance is %s%s%d.%2d\n", sign, currencysym,
dollarsandcents.quot, dollarsandcents.rem );
This code produces the following output:
The balance is -$12.34
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.