在cstdlib头文件中定义了abs(),labs(),llabs()函数。这些函数返回作为其参数输入到它们的整数的绝对值。
-
abs()函数:此函数的输入是C中int类型的值和C++中int类型,long int或long long int的值。在C中,输出是int类型,在C++中,输出具有与输入相同的数据类型。下面是示例C++程序,以显示abs()函数的工作。
// CPP program to illustrate // abs() function #include <cstdlib> #include <iostream> using namespace std; int main() { int val1, val2; /// finding absolute value using /// abs() function. val1 = abs(22); val2 = abs(-43); cout << "abs(22) = " << val1 << "\n"; cout << "abs(-43) = " << val2 << "\n"; return 0; }
Output: abs(22) = 22 abs(-43) = 43
-
labs()函数:这是abs()函数的long int版本。输入和输出均为long int类型。下面是示例C++程序,以显示labs()函数的工作。 post_top_responsive
// CPP program to illustrate // labs() function #include <cstdlib> #include <iostream> using namespace std; int main() { int val1, val2; /// finding absolute value using /// labs() function. val1 = labs(1234355L); val2 = labs(-4325600L); cout << "labs(1234355L) = " << val1 << "\n"; cout << "labs(-4325600L) = " << val2 << "\n"; return 0; }
Output: labs(1234355L) = 1234355 labs(-4325600L) = 4325600
-
llabs()函数:这是abs()函数的long long int版本。输入和输出均为long long int类型。下面是示例C++程序,以显示llabs()函数的工作。
// CPP program to illustrate // llabs() function #include <cstdlib> #include <iostream> using namespace std; int main() { int val1, val2; /// finding absolute value using /// labs() function. val1 = llabs(1234863551LL); val2 = llabs(-432592160LL); cout << "llabs(1234863551LL) = " << val1 << "\n"; cout << "llabs(-432592160LL) = " << val2 << "\n"; return 0; }
Output: llabs(1234863551LL) = 1234863551 llabs(-432592160LL) = 432592160
相关用法
注:本文由纯净天空筛选整理自nik1996大神的英文原创作品 abs(), labs(), llabs() functions in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。