The abs() function is used in C language to represent absolute values. This function supports int, double and float type data and returns the absolute value of the same input data type.
Representation of absolute value in C language
How to express absolute value?
In C language, the abs()
function is used directly to represent absolute values.
Function definition
<code class="c">#include <stdlib.h> int abs(int x); double abs(double x); float abs(float x);</code>
Function parameters
x
: To take the absolute value Number (can be of type int
, double
or float
) Return value
# Absolute value of)
Example
<code class="c">#include <stdio.h> #include <stdlib.h> int main() { int x = -5; double y = -3.14; float z = -2.5f; printf("绝对值 %d = %d\n", x, abs(x)); printf("绝对值 %f = %f\n", y, abs(y)); printf("绝对值 %f = %f\n", z, abs(z)); return 0; }</code>
Output
<code>绝对值 -5 = 5 绝对值 -3.140000 = 3.140000 绝对值 -2.500000 = 2.500000</code>
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!