Floating-point (decimal) is also called a real or floating-point number. For example, 0.0, 75.0, 4.023, 0.27, 937.198 are all valid decimals. This is a common form of decimals, called the decimal form.
In addition to the decimal form, you can also use an exponential form, such as 7.25x102, 0.0368x105, 100.22x10-2, and so on. Any decimal can be expressed in exponential form.
Decimals in the C language also have these two representations. In writing, the decimal form is the same as in mathematics, and the exponential form differs.
In the C language the exponential form of the decimal number is:
AEn or AEn
A is an end part, is a decimal number, n is an exponential part, is a decimal integer, E or E is a fixed character whose value is ax10n. For example:
- 2.1E5 = 2.1x105, where 2.1 is the mantissa and 5 is the exponent.
- 3.7E-2 = 3.7x10-2, where 3.7 is the mantissa,-2 is the exponent.
- 0.5E7 = 0.5x107, where 0.5 is the mantissa and 7 is the exponent.
The data type of the C language decimal is float or double:float is called a single-precision floating-point number, double is called a dual-precision floating-point. Unlike integers, the length of a decimal is always fixed, and float occupies 4 bytes, and a double occupies 8 bytes.
10 is fixed and does not need to be reflected in memory. The sign, exponent (n), and Mantissa (a) are variable and require memory space to represent them.
The form of float, double in memory is as follows:
Output float uses the%f control character, and the output double uses the%LF control, as follows:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- float a=128.101;
- float B=0. 302f;
- float C=1. 23002398f;
- Double D=123;
- double E = 78.429;
- printf("a=%f \b=%f \c=%f \d=%lf \e=%lf\ n", a , b, C, D, E);
- system("pause");
- return 0;
- }
Operation Result:
a=128.100998
b=0.302000
c=1.230024
d=123.000000
e=78.429000
Description of the code:
1)%f By default, six decimal places are reserved, less than six bits are 0 padded, and more than six bits are truncated by rounding.
2) When an integer is assigned to a float variable, it is converted to a decimal number.
3) Decimals default to double type, plus the suffix f is the float type.
4) due to limited memory, decimal precision is limited, so output a can only get an approximate number.
"C Language" 01-basic data type-floating point type