C++ Standard Library C++ STL Library

C++ <cstdlib> - labs() Function



The C++ <cstdlib> labs() function returns the absolute value (positive value) of the specified integral value. For example - absolute value of integral value x will be |x|.

Syntax

long int labs (long int x);

Parameters

x Specify an integral value whose absolute value need to be determined.

Return Value

Returns the absolute value (positive value) of integral argument.

Example:

The example below shows the usage of <cstdlib> labs() function.

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main (){
  cout<<"labs(10) = "<<labs(10)<<"\n";
  cout<<"labs(-10) = "<<labs(-10)<<"\n";
  cout<<"labs(50) = "<<labs(-50)<<"\n";
  cout<<"labs(-50) = "<<labs(50)<<"\n";  
  return 0;
}

The output of the above code will be:

labs(10) = 10
labs(-10) = 10
labs(50) = 50
labs(-50) = 50

❮ C++ <cstdlib> Library