You are here

abs in C

abs (isn't a function but a macro) is used for calculating the absolute value of a number.

abs function in C language

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

int main()
{
  int n, result;

  printf("Enter an integer to calculate its absolute value\n");
  scanf("%d", &n);

  result = abs(n);

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

  return 0;
}

Output of program:
abs math.h c programming

You can implement you own function as follows:

long absolute(long value) {
  if (value < 0)
    return -value;

  return value;  
}

Share this