# Absolute value in C

Absolute value describes the distance of a number on the number line from 0 without considering the direction. The absolute value of a number is always positive (distance can never be negative). Here we will develop a program to find the absolute value in C. First, we will develop a program without using any pre-defined function, and later we will develop the C program to find absolute value using the pre-defined abs() function.

Example:- The absolute value of -8 is 8. The absolute value of 5 is 5, and the absolute value of 0 is zero. Similarly, the absolute value of -18.5 is 18.5

## abs() function

The abs() function returns the absolute value of an integer. This function is defined under `stdlib.h` header file.

Function prototype of abs():-
`int abs(int x);`

• Required header file:- `"stdlib.h"`
• parameter:- int value or int variable
• Return value:- It returns the absolute value of the given integer number.
``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;

printf("Enter a number: ");
scanf("%d",&n);

n=abs(n);
printf("Absolute value = %d\n", n);

return 0;
}``````

Output for test1:-

Enter a number: -20
Absolute value = 20

Output for test2:-

Enter a number: 36
Absolute value = 36

## labs() function to find absolute value in C

The labs() function is defined in the “stdlib.h” header file, and it is used to find out the absolute value of long integer number in C programming.

Function prototype:-
`long int labs(long int x);`

Required header file:- `"stdlib.h"`
Parameter:- long int value or variable
Return value:- Absolute value of the given parameter in long int type

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
long int n;

printf("Enter a long integer number: ");
scanf("%ld", &n);

n = abs(n);
printf("Absolute value = %ld\n", n);

return 0;
}``````

Output:-

Enter a long integer number: -545514351
Absolute value = 545514351

## fabs() function

It is given in “math.h” header file, and it is used to find out the absolute value of the floating-point number in C programming.

The function prototype is:-
`double fabs(double x);`

Required header file:- `"math.h"`
Parameter:- double value or variable
Return value:- Absolute value of the given parameter in double type

``````#include<stdio.h>
#include<math.h>
int main()
{
double number;
double result;

printf("Enter a number: ");
scanf("%lf", &number);

result = fabs(number);
printf("Absolute value = %lf\n", result);

return 0;
}``````

Output:-

Enter a number: -51.508
Absolute value = 51.508000

## Absolute value in C without using the predefined function

``````#include<stdio.h>
int main()
{
double n;

printf("Enter a number: ");
scanf("%f",&n);

if(n<0.0) n = -n ;

printf("Absolute value = %f\n", n);

return 0;
}``````

Output:-

Enter a number: -52.8
Absolute value = 52.800000

Enter a number: 5
Absolute value = 5.000000

In this C program, we use the if block statement. If the number is negative then convert the number into a positive number otherwise it will remain as it is. To convert the number from negative to positive the minus operator (-) can be used. Finally, the result is displayed to the screen using the printf function.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

### 1 thought on “Absolute value in C”

1. I truly wanted to post a brief comment to be able to thank you for some of the lovely pointers you are sharing at this site. My incredibly long internet search has at the end of the day been honored with excellent facts and techniques to share with my good friends. I’d express that most of us site visitors are really lucky to exist in a wonderful site with so many special people with insightful tips and hints. I feel rather blessed to have come across your site and look forward to so many more entertaining moments reading here. Thanks a lot once more for all the details.