llabs() Function
The llabs()
function is declared in the header file <stdlib.h>
.
The llabs()
function returns the absolute value of a given integral value, ensuring that the result is always non-negative. This function is specifically designed to work with values of type long long int
, providing a long long version of the standard abs()
function.
Syntax of llabs()
long long int llabs(long long int n);
Parameters
Parameter | Description |
---|---|
n | An integral value of type long long int whose absolute value is to be computed. |
It is worth noting that this function computes the absolute value of the input, returning the non-negative value of n
. This can be particularly useful when dealing with large integers that require a wider range than the standard int
type.
Return Value
The function returns the absolute value of n
as a long long int
.
Examples for llabs()
Example 1: Basic Absolute Value Computation
This example demonstrates the basic usage of llabs()
by computing the absolute value of a negative number.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
long long int num = -123456789012345LL;
long long int absValue = llabs(num);
printf("The absolute value of %lld is %lld.\n", num, absValue);
return 0;
}
Explanation:
- A
long long int
variablenum
is initialized with a negative value. - The
llabs()
function is called to compute the absolute value ofnum
. - The absolute value is stored in
absValue
and then printed.
Program Output:
The absolute value of -123456789012345 is 123456789012345.
Example 2: Handling Zero and Positive Numbers
This example checks how llabs()
works with zero and a positive number, ensuring that the function correctly returns the input when it is non-negative.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
long long int values[] = {0, 98765432109876LL};
int i;
for (i = 0; i < 2; i++) {
printf("The absolute value of %lld is %lld.\n", values[i], llabs(values[i]));
}
return 0;
}
Explanation:
- An array
values
is initialized with twolong long int
values: zero and a positive number. - The program iterates through the array, using
llabs()
to compute and print the absolute value for each element. - The function returns the same value when the input is non-negative.
Program Output:
The absolute value of 0 is 0.
The absolute value of 98765432109876 is 98765432109876.
Example 3: Using llabs() in a Function
This example demonstrates how to integrate llabs()
within a custom function that processes an array of long long int
values by converting each element to its absolute value.
Program
#include <stdio.h>
#include <stdlib.h>
void printAbsoluteValues(long long int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("Absolute value of %lld is %lld.\n", arr[i], llabs(arr[i]));
}
}
int main() {
long long int numbers[] = {-10LL, 20LL, -30LL, 40LL, -50LL};
int n = sizeof(numbers) / sizeof(numbers[0]);
printAbsoluteValues(numbers, n);
return 0;
}
Explanation:
- A helper function
printAbsoluteValues()
is defined to iterate over an array oflong long int
values. - The function calls
llabs()
for each element to compute its absolute value and prints the result. - The main function initializes an array of both negative and positive numbers and calls the helper function.
Program Output:
Absolute value of -10 is 10.
Absolute value of 20 is 20.
Absolute value of -30 is 30.
Absolute value of 40 is 40.
Absolute value of -50 is 50.