C abs() function

C abs() function - Calculate integer absolute value

The abs() function is used to compute the absolute value of an integer value.

Syntax abs() function

int abs(int x)

Parameters abs() function

Name Description Required /Optional
x Numeric value. Required

Return value from abs()

  • Absolute value of its integer operand.

Example: abs() function

The following example shows the usage of abs() function.

#include <stdlib.h>
#include <stdio.h>
int main(void)
    int x, y;
    x = -347;
    y = abs(x);
    printf("The absolute value of %d is %d\n", x, y);
    x = 456; 
    y = abs(x);
    printf("\nThe absolute value of %d is %d\n", x, y); 


The absolute value of -347 is 347

The absolute value of 456 is 456

C Programming Code Editor:

Previous C Programming: C qsort()
Next C Programming: C div()

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
    ptr = 0;// oops, I meant *ptr = 0

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q