C++ cstdlib abs()

The abs() function in C++ returns the absolute value of an integer number. This function is defined in the cstdlib header file.

Mathematically, abs(num) = |num|.

Example


abs() Syntax

The syntax of the abs() function is:

abs(int num);

abs() Parameters

The abs() function takes the following parameters:

  • num: An integral value whose absolute value is returned. The number can be:
    • int
    • long
    • long long

abs() Return Value

The abs() function returns:

  • the absolute value of num i.e. |num|
  • the positive value if the specified number is negative

abs() Prototypes

The prototypes of abs() as defined in the cstdlib header file are:

int abs(int num);

long abs(long num);

long long abs(long long num);

abs() Overloading

The abs() function is also overloaded in:

  • cmath header file for floating-point types
  • complex header file for complex numbers
  • valarray header file for valarrays

Example: C++ abs()

Output

abs(-5) = |-5| = 5
abs(-2371041) = |-2371041| = 2371041