C++ labs()

The labs() function can be thought as the long int version of abs().

It is defined in <cstdlib> header file.

[Mathematics] |x| = labs(x) [C++ Programming]

labs() prototype [As of C++ 11 standard]

long labs(long x);
long int labs(long int x);

The labs() function takes a single argument of type long or long int and returns a value of same type.


labs() Parameters

x: A long or long int data whose absolute value is returned.


labs() Return value

The labs() function returns the absolute value of x i.e. |x|.


Example: How labs() function works in C++?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	long int x,y;
	x = -9999999L;
	y = 10000000L;

	cout << "labs(" << x << ") = |" << x << "| = " << labs(x) << endl;
	cout << "labs(" << y << ") = |" << y << "| = " << labs(y) << endl;

	return 0;
}

When you run the program, the output will be:

labs(-9999999) = |-9999999| = 9999999
labs(10000000) = |10000000| = 10000000

Also Read:

Did you find this article helpful?