How do you change negative numbers to positive numbers in the C language?

We can turn negative numbers into positive ones by using the absolute value function. In C language, we can use the abs() function to calculate the absolute value of an integer. For example:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int num = -5;
    int result = abs(num);

    printf("原始数值:%d\n", num);
    printf("绝对值:%d\n", result);

    return 0;
}

The output result is:

原始数值:-5
绝对值:5

This changes the negative number -5 into the positive number 5.

bannerAds