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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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().
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more