- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The abs(), labs(), llabs() functions in C/C++
What are Integer Functions in C Library?
Integer functions are those functions which returns the exact value of an integer. C only supports integer values. In this function the nearest integer which is less than or equal to the argument returns to this function.
Types of Integer functions −
int = abs (int n); long = labs (long n); long long = llabs (long long n);
where n = integer value
What is abs(), labs(), llabs() functions ?
They are defined as <cstdlib> (C Standard General Utilities Library) header file. They give the exact value of integer that is input to them as their argument.
abs() function − In C the input is of type ‘int’ whereas in C++ input is of type ‘ int, long int or long long int’. In C the output is of ‘int’ type and in C++ the output has the same data type as input.
Basically the abs function evaluates the absolute value of the given value i.e. value after removing all the signs of negative and positive from the number. Which means it will always return a positive number.
For example,
abs(-43) will give 43 as output as it is created to remove the negative sign.
abs(12) will give 12 as output as there is no sign that need to be removed.
Example
#include <cstdlib> #include <iostream> using namespace std; int main() { int a = abs(123); int b = abs(-986); cout << "abs(123) = " << a << "\n"; cout << "abs(-986) = " << b << "\n"; return 0; }
Output
abs(123) = 123 abs(-986) = 986
labs() function − In this function the type of both the input and output are of long int and this is the long int version of abs() function.
The function is the same as abs() i.e. removing the negative of the number but the difference is this method can handle long values.
For example,
labs(245349384932L) = 245349384932
labs(-34235668687987) = 34235668687987
Example
#include <cstdlib> #include <iostream> using namespace std; int main() { long int a = labs(437567342L); long int b = labs(-8764523L); cout << "labs(437567342L) = " << a << "\n"; cout << "labs(-8764523L) = " << b << "\n"; return 0; }
Output
labs(437567342L) = 437567342 labs(-8764523L) = 8764523
llabs() function − In this function the type of both the input and output are of long long int and this is the long long int version of abs() function.
Example
#include <cstdlib> #include <iostream> using namespace std; int main() { long long int a = llabs(9796546325253547656LL); long long int b = llabs(-1423446557676111567LL); cout << "llabs(9796546325253547656LL) = " << a << "\n"; cout << "llabs(-1423446557676111567LL) = " << b << "\n"; return 0; }
Output
llabs(9796546325253547656LL) = 9796546325253547656 llabs(-1423446557676111567LL) = 1423446557676111567