abs labs llabs in c

abs labs llabs functions in C/C++

The abs, labs, llabs functions compute the absolute value of an integer. These functions abs, labs, llabs are defined in stdlib.h header file.

The behavior of these functions is undefined if the result cannot be represented by the return type. So let see these function one by one with some example code.

abs():

The abs functions take an int as an argument and compute its absolute value. Let’s see the syntax of the abs function.

//Syntax of abs function.

int abs(int i);

Parameters:

i— integer value

Return:

The abs function returns the absolute value ‘i’ if it is representable.

 

Let’s see an example code to understand the functionality of the abs in C. In this C code, I am calculating the absolute value of given int value.

#include <stdio.h>
#include<stdlib.h>

int main()
{
   //passing 27
    printf("abs(27) = %d\n", abs(27));

    //passing -27
    printf("abs(-27) = %d\n", abs(-27));

    return 0;
}

When you run the program, the output will be:

abc in c

 

Now let’s see another example where I want to compute the absolute value INT_MIN (Min integer value).

#include <stdio.h>
#include<stdlib.h>
#include <limits.h>

int main()
{
    //INT_MIN the max value store by
    //integer variable
    printf("%d\n", abs(INT_MIN));
    
    return 0;
}

When you run the program, the output will be:

The program behavior is undefined because of the absolute value of INT_MIN is out of range of positive integer number.

 

labs():

The labs functions take an long int as an argument and compute its absolute value. Let’s see the syntax of the abs function.

//Syntax of labs


long int labs(long int i);

Parameters:

i— long int value

Return:

The labs function returns the absolute value ‘i’ if it is representable.

 

Below is the sample C program to show the working of labs() function.

#include <stdio.h>
#include<stdlib.h>

int main()
{
    //passing 12345
    printf("labs(12345) = %ld\n", labs(12345));

    //passing -12345
    printf("labs(-12345) = %ld\n", labs(-12345));

    return 0;
}

Output:

labs(12345) = 12345

labs(-12345) = 12345

 

Now let’s see another example where I want to compute the absolute value LONG_MIN (Min value of long integer).

#include <stdio.h>
#include<stdlib.h>
#include <limits.h>

int main()
{
    //LONG_MIN the max value store by
    //integer variable
    printf("%ld\n", labs(LONG_MIN));

    return 0;
}

When you run the program, the output will be:

The program behavior is undefined because of the absolute value of LONG_MIN is out of range of a positive long int.

 

llabs():

The llabs functions define in C99 and onwards. It takes a long long int as an argument and computes its absolute value. Let’s see the syntax of the llabs function.

//syntax of llabs

long long int llabs(long long int j);

Parameters:

i— long long int value

Return:

The llabs function returns the absolute value ‘i’ if it is representable.

 

Below is the sample C program to show the working of llabs() function.
#include <stdio.h>
#include<stdlib.h>

int main()
{
    //passing 9876543210
    printf("llabs(9876543210) = %lld\n", llabs(9876543210LL));

    //passing -9876543210
    printf("llabs(-9876543210) = %lld\n", llabs(-9876543210LL));

    return 0;
}

Output:

llabs(9876543210) = 9876543210

llabs(-9876543210) = 9876543210

 

Now let’s see another example where I want to compute the absolute value LLONG_MIN (Min value of long long int).

#include <stdio.h>
#include<stdlib.h>
#include <limits.h>


int main()
{
    //LLONG_MIN the max value store by
    //integer variable
    printf("%lld\n", llabs(LLONG_MIN));

    return 0;
}

When you run the program, the output will be:

The program behavior is undefined because of the absolute value of LLONG_MINis out of range of a positive long long int.