Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
div
eof
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

abs, _abs64

Updated: September 2010

Calculate the absolute value.

int abs( 
   int n 
);
long abs( 
   long n 
);   // C++ only
double abs( 
   double n 
);   // C++ only
long double abs(
   long double n
);   // C++ only
float abs(
   float n 
);   // C++ only
__int64 _abs64( 
   __int64 n 
);

n

Integer value.

The abs function returns the absolute value of its parameter. There is no error return.

C++ allows overloading, so you can call overloads of abs. In a C program, abs always takes and returns an int.

Caution noteCaution:

Both abs(INT_MIN) and _abs64(INT_MIN) return a value of INT_MIN. Although this is the only time that abs and _abs64 return a negative value, it means that abs and _abs64 cannot be used to guarantee a positive value.

Routine

Required header

abs

<math.h>

_abs64

<stdlib.h>

This program computes and displays the absolute values of several numbers.

// crt_abs.c
// This program demonstrates the user of the abs function
// by computing and displaying the absolute values of
// several numbers.

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

int main( void )
{
    int     ix = -4,
            iy;
    long    lx = -41567L,
            ly;
    double  dx = -3.141593,
            dy;
    __int64 wx = -1, wy;

    // absolute 64 bit integer value
    wy = _abs64( wx );
    printf_s( "The absolute value of %I64x is %I64x\n", wx, wy);

    // absolute 32 bit integer value
    iy = abs( ix );
    printf_s( "The absolute value of %d is %d\n", ix, iy);

    // absolute long integer value
    ly = labs( lx );
    printf_s( "The absolute value of %ld is %ld\n", lx, ly);

    // absolute double value
    dy = fabs( dx );
    printf_s( "The absolute value of %f is %f\n", dx, dy );
}
The absolute value of ffffffffffffffff is 1 The absolute value of -4 is 4 The absolute value of -41567 is 41567 The absolute value of -3.141593 is 3.141593

Date

History

Reason

September 2010

Added a note about INT_MIN.

Information enhancement.

Community Additions

ADD
Show:
© 2015 Microsoft