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.
#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!