abs c - math.h


abs is not a function but is a macro and is used for calculating absolute value of a number.

C programming code for abs
#include <stdio.h>
#include <math.h>

int main()
{
  int n, result;

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

  result = abs(n);

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

  return 0;
}
Output of program:

You can implement you own function as follows:

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

Comments

Popular posts from this blog

Difference between asterisk and ampersand operators in c

kbhit in c