December 2005
Beginner to intermediate
618 pages
20h 19m
English
abs
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.
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
Read now
Unlock full access