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 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

WBOY
WBOYforward
2023-08-26 13:49:02920browse

在C/C++中,abs()、labs()和llabs()函数的翻译如下:

abs()函数用于返回一个整数的绝对值。
labs()函数用于返回一个长整数的绝对值。
llabs()函数用于返回一个长长整数的绝对值

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.

abs() function

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)

Example

#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;
}

Output

Absolute value of -145 is: 145
Absolute value of 145 is: 145

labs() function

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)

Example

#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;
}

Output

Absolute value of -9256847 is: 9256847
Absolute value of 9256847 is: 9256847

llabs() function

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)

Example

#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;
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete