What is imaxabs() in C?

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:

The absolute value of both n and -n is n.

To use the imaxabs function, the inttypes.h header file needs to be included in the program, as shown below:

#include <inttypes.h>

Syntax

The imaxabs function requires a single argument and returns a single value. The function declaration is as follows:

Parameters

The imaxabs function takes a single integer value of intmax_t type as the argument.

Return Value

The imaxabs returns the absolute value of the argument, i.e., |n|.

Example

The code below shows the use of the imaxabs function in C:

Note: the imaxabs function is equivalent to the abs function in C. However, the abs function takes and returns the int type, whileimaxabs takes and returns the intmax_t type.

#include <stdio.h>
#include <inttypes.h>
int main() {
//Declare intmax_t variables
intmax_t a = 123456;
intmax_t b = 789234;
//Substract the 2 values
intmax_t sub = a - b;
printf ("The substracted result is: %ji\n", sub);
//Find the absolute value of sub
intmax_t abs = imaxabs(sub);
printf ("The absolute result is: %ji\n", abs);
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved