×

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

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

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

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and...

4 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++ 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.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.