Home > Article > Backend Development > In C/C++, the abs(), labs() and llabs() functions are translated as follows: The abs() function is used to return the absolute value of an integer. The labs() function is used to return the absolute value of a long integer. The llabs() function is used to return the absolute value of a long integer
In the C cstdlib library, in addition to abs, there are different functions for obtaining absolute values. In C, abs is basically used for int type input, in C, for int, long, long long. Others are used for long, long long type data, etc. Let's see the usage of these functions.
This function is used for int type data. So this returns the absolute value of the given parameter. The syntax is as follows.
int abs(int argument)
#include <cstdlib> #include <iomanip> #include <iostream> using namespace std; main() { int x = -145; int y = 145; cout << "Absolute value of " << x << " is: " << abs(x) << endl; cout << "Absolute value of " << y << " is: " << abs(y) << endl; }
Absolute value of -145 is: 145 Absolute value of 145 is: 145
This function is used for long type data. So this returns the absolute value of the given parameter. The syntax is as follows.
long labs(long argument)
#include <cstdlib> #include <iomanip> #include <iostream> using namespace std; main() { long x = -9256847L; long y = 9256847L; cout << "Absolute value of " << x << " is: " << labs(x) << endl; cout << "Absolute value of " << y << " is: " << labs(y) << endl; }
Absolute value of -9256847 is: 9256847 Absolute value of 9256847 is: 9256847
This function is used for long long type data. So this returns the absolute value of the given parameter. The syntax is as follows.
long long labs(long long argument)
#include <cstdlib> #include <iomanip> #include <iostream> using namespace std; main() { long long x = -99887654321LL; long long y = 99887654321LL; cout << "Absolute value of " << x << " is: " << llabs(x) << endl; cout << "Absolute value of " << y << " is: " << llabs(y) << endl; }
Absolute value of -99887654321 is: 99887654321 Absolute value of 99887654321 is: 99887654321
The above is the detailed content of In C/C++, the abs(), labs() and llabs() functions are translated as follows: The abs() function is used to return the absolute value of an integer. The labs() function is used to return the absolute value of a long integer. The llabs() function is used to return the absolute value of a long integer. For more information, please follow other related articles on the PHP Chinese website!