C + + basic problem--float type output in integer format

Source: Internet
Author: User

1 intMain ()2 {3     floatA =765;4a++;5printf"%d\n", a);6System"Pause");7     return 0;8 }9 //why would the output be 0? Ten The explanations are as follows: OneWhen you are in printf,floatis automatically converted to double type, from 32 bits to 64 bits. %d output is the lowest of 32 bits.  A The computer is a small terminal mode, so when saving 765, it is the following format - bottom of the stack (high-byte) ....... Stack top (low byte) - 765                           0000 the The following question is extended - intMain () { -intAfloatb,c; -scanf"%2d%3f%4f",&a,&b,&c); +printf"\na=%d,b=%d,c=%f\n", a,b,c); - } +If you enter 9876543210l from the keyboard at runtime, the output from the above program under the GCC compiler is a=98, b=0, c=0.000000 A parsing: at when printf, float is automatically converted to double type, so variable b and variable C go from 32 to 64 bits; -B is in%d in the format of the output, so the output is the lowest 32 bits.  -The computer is a small-end mode-High-byte high address, low-byte low address - printf presses the stack in order from right to left, thus first pressing C, then B, then A; - bottom of the stack (high-byte) ....... Stack top (low byte) - 4321     0000      765     0000         98 in 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes - The output starts at low byte, to first output A, four bytes, i.e. 98 + then output B, four bytes, or 0 - then output C, eight bytes, is the normal way to print, will read eight bytes at a time, exactly the first four bytes are all 0. Because of the way floating-point numbers are stored, theThe 64-bit contains 1 sign bits, 11 orders, 52 Mantissa, order 0, indicating-1023+1023=0, denoted by an exponent:1.#*2^-1023, * ' # ' represents the mantissa. The Mantissa is 000 ... The number behind is small, so this floating point is very small. This problem can be said to be the output mode of B influence $C, if B is output with%f, then B and C will display correctly

C + + basic problem--float type output in integer format

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.