abs(), labs(), llabs() functions in C/C++ - GeeksforGeeks

26 Apr, 2018
abs(), labs(), llabs() functions are defined in cstdlib header file. These functions return the absolute value of integer that is input to them as their argument.
abs() function: Input to this function is value of type int in C and value of type int, long int or long long int in C++. In C output is of int type and in C++ the output has same data type as input.Below is the sample C++ program to show working of abs() function.// 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() function: This is the long int version of abs() function. Both the input and output are of long int type.Below is the sample C++ program to show working of labs() function.// 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() function: This is the long long int version of abs() function. Both the input and output are of long long int type.Below is the sample C++ program to show working of llabs() function.// 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
My Personal Notes
arrow_drop_upSave
abs() function: Input to this function is value of type int in C and value of type int, long int or long long int in C++. In C output is of int type and in C++ the output has same data type as input.Below is the sample C++ program to show working of abs() function.// 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

// 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() function: This is the long int version of abs() function. Both the input and output are of long int type.Below is the sample C++ program to show working of labs() function.// 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

// 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() function: This is the long long int version of abs() function. Both the input and output are of long long int type.Below is the sample C++ program to show working of llabs() function.// 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
My Personal Notes
arrow_drop_upSave
// 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

CPP-Library
STL
C++
Technical Scripter
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Classes and Objects
Virtual Function in C++
Constructors in C++
Templates in C++ with Examples
Operator Overloading in C++
Socket Programming in C/C++
Polymorphism in C++
Copy Constructor in C++
Friend class and function in C++
C++ Data Types