The imaxabs function is a C library function that computes the absolute value of the intmax_t type.
The intmax_t is the longest integer type in C – it’s the same as the long or the long long type.
The absolute value of a number (n) is the non-negative value of n. In mathematics, we denote the absolute value as below:
To use the imaxabs function, the inttypes.h header file needs to be included in the program, as shown below:
#include <inttypes.h>
The imaxabs function requires a single argument and returns a single value. The function declaration is as follows:
The imaxabs function takes a single integer value of intmax_t type as the argument.
The imaxabs returns the absolute value of the argument, i.e., |n|.
The code below shows the use of the imaxabs function in C:
Note: the
imaxabsfunction is equivalent to theabsfunction in C. However, theabsfunction takes and returns theinttype, whileimaxabstakes and returns theintmax_ttype.
#include <stdio.h>#include <inttypes.h>int main() {//Declare intmax_t variablesintmax_t a = 123456;intmax_t b = 789234;//Substract the 2 valuesintmax_t sub = a - b;printf ("The substracted result is: %ji\n", sub);//Find the absolute value of subintmax_t abs = imaxabs(sub);printf ("The absolute result is: %ji\n", abs);return 0;}
Free Resources