Printf function % F output int

Source: Internet
Author: User

In 《ProgramI saw a question about printf in the staff interview book. It's interesting to record it.

float value = 1 . 0 ;
printf ( " value_int = % d \ n " , value ) ;

Printf ("value_float = % F \ n", value );


What should I output? At first glance, this question is very simple. The storage format of floating point number 1.0 in the memory is 0x3f800000. The float type occupies 4 bytes in the memory, and the int type occupies 4 bytes. You can simply output the 0x3f800000 decimal format.
Who knows how to run the big drop glasses, print information:
Value_int = 0
Value_float = 1.0
What's going on?
Use the GCC-S parameter to convert the. C program into the. s assembly language program. We can see that:

FLDS - 8 ( % EBP )
fstpl 4 ( % ESP )
movl $ . LC1 , ( % ESP )
call printf


The value is stored in-8 (% EBP), and the "value = % d \ n" string is stored in. LC1.
The FLDS command puts the single-precision value into the st7 register (64bit) of FPU. The value in st7 is 0x3f80000000000000.
The fstpl command then outputs the values in the FPU register in the form of Dual-precision, and stores them at 4 (% ESP. That is, the value in (% ESP + 4) is 0x00000000, and (% ESP + 8) is 0x3f800000.
When printf is called, because the specified printing method is % d, printf only reads the four bytes (% ESP + 4) and interprets them as decimal integer -- 0, the correct value (% ESP + 8) of 0x3f800000 is not taken into account.
You may also wonder why the float occupies only four bytes in the memory. Why is it true if % F is specified? The answer is that if you specify the printf output parameter as % F, then printf reads 8 bytes in the memory, not just 4 byte of the low address. Write a program to test it:

Int Main ( )
{
Int A = 1 , B = 2 , C = 3 ;
Printf ( "% F, % d \ n" , A , B , C ) ;
Return 0 ;
}


Output result:
0.00000, 3
Therefore, % F actually reads 8 bytes, but when a and B are converted to the float type, they are all output as 0.00000 because the value is too small.

Transferred from:Http://blog.chinaunix.net/space.php? Uid = 22516719 & Do = Blog & id = 263225

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.