abs() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In the C programming language, the use of the function abs in C is to return the absolute value of an integer. By integer, it means that the abs() is an arithmetics function. The stdlib.h library contains predefined function abs() for computing the absolute value of integers.

By absolute value, it means the function returns the positive value of an integer. Let's say we have an int value -3, and we want to know what the absolute value of -3 is. So we will use the absolute function and return the absolute value of -3, which is 3.

Let's have a look at the syntax of abs() function :

Syntax of abs() in C

In the C programming language, abs() has the following syntax

Parameters of abs() in C

The parameter or argument of abs() function is an integral value. In the above syntax, it is x.

Return Value of abs() in C

Whatever integer value we pass into the absolute function abs(), the absolute value of that integer is the return value of abs() in C. In the above, the return value of abs() is the absolute value of x.

Example

A single and short code example to get the absolute value of a number using the abs() function.

Program:

Output:

What is abs in C?

The abs function in the C programming language stands for "Absolute Value". In other words, it's the distance between a number and a number line beginning at 0, without taking the direction into account. A number will always have a positive abs value or absolute value, meaning that a distance will never be negative.

Required Header

To use the abs() function in C, you need a header file called <stdlib.h>.

Applies To

You can use the abs function in the following C versions :

  • ANSI/ISO98991990ANSI/ISO 9899-1990

More Examples

Program to Get the Absolute Value of a Number Using the Function abs in C

Program:

Output:

Program to Print the Absolute Values of the Given Integers Using abs in C

Output:

Program to Print the Absolute Values Between Two Integers Using for Loop

Program:

Output:

Program to Get the Absolute Value Without Using the Function abs in C

Program:

Output:

Conclusion

Let's summarize what we have learned in this tutorial,

  • The use of the function abs in C programming is to return the absolute value of an integer.
  • By absolute value, it means the function returns the positive value of an integer.
  • The parameter or argument of function abs in C is an integral value.
  • To use the abs() function in C, you need a header file called <stdlib.h>.
  • Absolute value of the integer is returned by abs().