You are here

abs c - math.h

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

C programming code for abs

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6.   int n, result;
  7.  
  8.   printf("Enter an integer to calculate it's absolute value\n");
  9.   scanf("%d", &n);
  10.  
  11.   result = abs(n);
  12.  
  13.   printf("Absolute value of %d = %d\n", n, result);
  14.  
  15.   return 0;
  16. }

Output of program:
abs math.h c programming

You can implement you own function as follows:

  1. long absolute(long value) {
  2.   if (value < 0) {
  3.     return -value;
  4.   }
  5.   else {
  6.     return value;  
  7.   }
  8. }