×

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned as it is. The abs in C++ is described in <cstdlib> header file. The absolute value in C++ works similar to modulus operator in Mathematics: abs(a) = |a|.

Syntax of abs() in C++:

abs ( int a); 

Example:

#include <iostream>
#include <cstdlib>
using namespace std;


int main() {


  //absolute value of -5
cout<< abs(-5);


return 0;
}

Output:

5

Parameters of abs() in C++

The abs () function in C++ takes only one parameter, but they are of three data types:

  • Int
  • long int
  • longlong int

Return Values of abs() in C++

In C++, the abs() function returns the absolute value of a number. The data type value that is returned by abs in C++ is equal to the data type of the argument passed, that means it could be int, long int, or even long long int.

Example for abs() in C++:

//header files
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int a = -42;
int a1 = 42;
int a2 = 0;
 /// Using abs() function on a, a1, a2.
int ab = abs(a);
int ab1 = abs(a1);
int ab2 = abs(a2);
cout<<"abs () value of "<<a<< " is: "<< ab << "\n";
cout<<"abs () value of "<<a1<< " is: "<< ab1 << "\n";
cout<<"abs () value of "<<a2<< " is: "<< ab2 << "\n";
return 0;
}

Output:

abs () value of -42 is: 42
abs () value of 42 is: 42
abs() value of 0 is: 0

Explanation:

  • As we have discussed above, in C++, abs () returns the absolute value of the value passed. In the above code, for integer number a = -42, the absolute value of -42= |-42| = 42, so abs(-42) returned 42.
  • For integer number a1 = 42, the absolute value of 42 = |42| = 42, so abs(42)  returned 42.
  • For integer number a2 = 0, the absolute value of 0 = |0| = 0, hence absolute value of 0 is 0, so abs(0) will return 0.

Prototypes in abs()

int abs( int a);  or
long int abs(long int a);  or
longlong int abs(long long int a);  
  • If the num passed is of int type, then the return will be of int type.
  • If the num passed is of long type, then the return will be of long type.
  • If the num passed is of long long type, then the return will be of long long type.

Exceptions of abs() in C++

No-throw guarantee: This function usually throws no exceptions. If the result cannot be represented by the returned type in an implementation with two's complement signed values, it causes undefined behaviour.

For Example: abs is called on INT_MIN = -2147483648. The range of int in C++ is from -2147483648 to 2147483647. So if abs(-2147483648) is called, it will do (-1)*(-2147483648), which will result in it being out of range of int. Thus it will give undefined behaviour.

abs() Overloading

The abs() in C++ can be overloaded to be used for floating, complex types.

  • cmath header file is to be included for floating-point types.
  • Complex header file is to be included for complex numbers.

What is cstdlib abs() in C++?

The abs() in C++ returns the absolute value of an integer number.

  • If the number is negative, it will return the positive value (with the same magnitude), for example, abs(-1) = 1.
  • If the number is already positive or zero, it will return the number as it is, for example, abs(1) = 1.

What is cstdlib fabs() in C++?

The fabs() in C++ returns the absolute value of an integer, float, or double-type value. In case of char type, it returns the ASCII code of that character.

  • If the number is negative, it will return the positive value (with the same magnitude), for example, abs(-1) = 1.
  • If the number is already positive or zero, it will return the number as it is, for example, abs(1) = 1.

Difference Between abs() and fabs() in C++

abs()fabs()
The abs() is used for integer data type.The fabs() is used for int, float, double, and char data types.
The abs() function is described in <cstdlib> header file.The fabs() function is described in <cmath> header file.

Related Topics

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.