Home >Backend Development >C#.Net Tutorial >How to express absolute value in C language

How to express absolute value in C language

hzc
hzcOriginal
2020-07-01 15:32:5851713browse

In C language, the absolute value can be represented by the library function fabs or abs. fabs means taking the absolute value of double type data, and abs means taking the absolute value of int type data.

How to express absolute value in C language

#In C language, absolute values ​​can be expressed using the library function fabs or abs.

fabs means taking the absolute value of double data.

abs means taking the absolute value of int type data.

The function prototype is: double fabs(double x).

Use baiabs() function for integers

For example:

#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
scanf("%d",&a);
b=abs(a);
printf("%d",b);
return 0;
}
输入-10,输出10。
有小数的(即浮点型)用fabs()函数
例如:
#include<stdio.h>
#include<math.h>
int main()
{
double a,b;
scanf("%lf",&a);
b=fabs(a);
printf("%lf",b);
return 0;
}
输入-1.2,输出1.2

Recommended tutorial: "c Language Tutorial"

The above is the detailed content of How to express absolute value in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn