Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

abs — absolute value of an int

The abs function from <stdlib.h> returns an int absolute value. Calling abs(INT_MIN) has undefined behavior.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdlib.h>
int abs(int j);

abs(j), declared in <stdlib.h>, accepts and returns int. For a negative argument, it returns the opposite value; zero and positive values remain unchanged.

The result must be representable as int. In C23, abs(INT_MIN) has undefined behavior: the positive counterpart of INT_MIN cannot fit in int. INT_MIN comes from <limits.h>; check this boundary before calling if input can reach it.

Examples

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("%d %d %d\n", abs(-7), abs(0), abs(7));
    return 0;
}

Result

7 0 7

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources