"C Language" 01-basic data type-floating point type

Source: Internet
Author: User

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:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. float a=128.101;
  6. float B=0. 302f;
  7. float C=1. 23002398f;
  8. Double D=123;
  9. double E = 78.429;
  10. printf("a=%f \b=%f \c=%f \d=%lf \e=%lf\ n", a , b, C, D, E);
  11. system("pause");
  12. return 0;
  13. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.