C++ cstdlib abs() Function
Example
Get the absolute (positive) value of different numbers:
cout << abs(-8) << "\n";
cout << abs(8) << "\n";
cout << abs(3 - 10);
Definition and Usage
The abs() function returns the absolute value of a number. The absolute value is the number without a minus sign, so it is never negative.
The abs() function in <cstdlib> works with whole numbers: int, long and long long.
The abs function is defined in the <cstdlib> header file.
Tip: For decimal numbers, like -4.7, use abs() from the <cmath> library.
Syntax
int abs(int x);
long abs(long x);
long long abs(long long x);
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. A whole number. |
Technical Details
| Returns: | The absolute (positive) value of x, with the same type as x. |
|---|