A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
abs returns the absolute value of an int. fabs returns the absolute value of a double. They are in different headers: abs is in stdlib.h, fabs is in math.h. For long, use labs. For long long, use llabs. For float, use fabsf. For long double, use fabsl. Using the wrong variant causes implicit conversions that can truncate or lose precision. The C standard is precise about which function handles which type.
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(void) {printf("abs(-42): %d\n", abs(-42));printf("labs(-100000L): %ld\n", labs(-100000L));printf("fabs(-3.14): %.4f\n", fabs(-3.14));printf("fabsf(-2.5f): %.4f\n", fabsf(-2.5f));int a = 10, b = 25;printf("Distance: %d\n", abs(a - b));return 0;}
abs(INT_MIN) is undefined behavior. The absolute value of INT_MIN (-2147483648) is 2147483648, which does not fit in a 32-bit signed int. The result overflows. Check for INT_MIN before calling abs if your input range includes it.
Absolute value functions are small utilities that appear in distance calculations, error measurements, and comparison tolerances. Use the type-correct variant. Watch out for INT_MIN. Next: sqrt and its variants, for computing square roots with type precision.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.