C programming labs and llabs stdlib.h


In C programming <stdlib.h> labs and labscompute the absolute value of the argument passed.The declaration of the function is given below.

1 long int labs( long int j );
2 long long int llabs( long long int j );

labs
Parameters:
j – The value to be computed to absolute value.

Return type
long int -The absolute value of the argument.

llabs
Parameters:
long long -The value to be computed to absolute value.

Return type
long long int-The absolute value of the argument passed.

Some points to note:

i) Use ‘llabs’ to support larger values for the return type.

ii) If the return value is not representable by the return type the output is undefined.

Code example

printf( “labs( -4567 )= %li”, labs( -4567 ) );

printf( “labs( -9288327278238328 )=%li” ,labs( -9288327278238328 ) ) ; ///Output is undefined

printf( “llabs( -9288327278238328 )=%lli”, llabs( -9288327278238328 ) ) ; ///Output is valid

printf( “llabs( -999999999998839999239999231999999 )=%lli”, llabs( -999999999998839999239999231999999 ) ) ; ///Output is undefined

Output

labs( -4567 )=4567
labs( -9288327278238328 )=939137656 (undefined )
llabs( -9288327278238328 )= -9288327278238328
llabs( -999999999998839999239999231999999 )= 6233766678070673407 (undefined )


Related links

->C programming div ldiv ,lldiv stdlib.h