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

How to express absolute value in C language

hzc
Release: 2020-07-01 16:15:25
Original
51681 people have browsed it

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template