• <i id='ca6i7'><tr id='ca6i7'><dt id='ca6i7'><q id='ca6i7'><span id='ca6i7'><b id='ca6i7'><form id='ca6i7'><ins id='ca6i7'></ins><ul id='ca6i7'></ul><sub id='ca6i7'></sub></form><legend id='ca6i7'></legend><bdo id='ca6i7'><pre id='ca6i7'><center id='ca6i7'></center></pre></bdo></b><th id='ca6i7'></th></span></q></dt></tr></i><div id='ca6i7'><tfoot id='ca6i7'></tfoot><dl id='ca6i7'><fieldset id='ca6i7'></fieldset></dl></div>

    1. <small id='ca6i7'></small><noframes id='ca6i7'>

        <legend id='ca6i7'><style id='ca6i7'><dir id='ca6i7'><q id='ca6i7'></q></dir></style></legend>

        • <bdo id='ca6i7'></bdo><ul id='ca6i7'></ul>
        <tfoot id='ca6i7'></tfoot>

      1. 这个程序是如何运作的?

        时间:2023-09-17
            <bdo id='iNE5E'></bdo><ul id='iNE5E'></ul>
          • <tfoot id='iNE5E'></tfoot>

                <tbody id='iNE5E'></tbody>

                <i id='iNE5E'><tr id='iNE5E'><dt id='iNE5E'><q id='iNE5E'><span id='iNE5E'><b id='iNE5E'><form id='iNE5E'><ins id='iNE5E'></ins><ul id='iNE5E'></ul><sub id='iNE5E'></sub></form><legend id='iNE5E'></legend><bdo id='iNE5E'><pre id='iNE5E'><center id='iNE5E'></center></pre></bdo></b><th id='iNE5E'></th></span></q></dt></tr></i><div id='iNE5E'><tfoot id='iNE5E'></tfoot><dl id='iNE5E'><fieldset id='iNE5E'></fieldset></dl></div>
                • <small id='iNE5E'></small><noframes id='iNE5E'>

                • <legend id='iNE5E'><style id='iNE5E'><dir id='iNE5E'><q id='iNE5E'></q></dir></style></legend>
                • 本文介绍了这个程序是如何运作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  #include <stdio.h>
                  
                  int main() {
                      float a = 1234.5f;
                      printf("%d
                  ", a);
                      return 0;
                  }
                  

                  它显示一个 0!!这怎么可能?这是什么道理?

                  It displays a 0!! How is that possible? What is the reasoning?

                  我特意在printf语句中放了一个%d来研究printf的行为.

                  I have deliberately put a %d in the printf statement to study the behaviour of printf.

                  推荐答案

                  那是因为 %d 需要一个 int 但你提供了一个浮点数.

                  That's because %d expects an int but you've provided a float.

                  使用 %e/%f/%g 打印浮点数.

                  Use %e/%f/%g to print the float.

                  关于为什么打印0:浮点数在发送到printf之前被转换为double.1234.5 小端双数表示法是

                  On why 0 is printed: The floating point number is converted to double before sending to printf. The number 1234.5 in double representation in little endian is

                  00 00 00 00  00 4A 93 40
                  

                  A %d 使用 32 位整数,因此打印零.(作为测试,您可以 printf("%d, %d ", 1234.5f); 您可以获得输出 0, 1083394560.)

                  A %d consumes a 32-bit integer, so a zero is printed. (As a test, you could printf("%d, %d ", 1234.5f); You could get on output 0, 1083394560.)

                  至于为什么将float转成double,因为printf的原型是int printf(const char*, ...),来自 6.5.2.2/7,

                  As for why the float is converted to double, as the prototype of printf is int printf(const char*, ...), from 6.5.2.2/7,

                  函数原型声明符中的省略号会导致参数类型转换在最后一个声明的参数之后停止.对尾随参数执行默认参数提升.

                  The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

                  从 6.5.2.2/6 开始,

                  and from 6.5.2.2/6,

                  如果表示被调用函数的表达式的类型不包含原型,则对每个参数执行整数提升,float类型的参数提升为double.这些被称为默认参数提升.

                  If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions.

                  (感谢 Alok 发现这一点.)

                  (Thanks Alok for finding this out.)

                  这篇关于这个程序是如何运作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:什么是“位填充"?或“填充位"确切地? 下一篇:内存管理、堆损坏和 C++

                  相关文章

                  最新文章

                    <bdo id='eYsRf'></bdo><ul id='eYsRf'></ul>

                  <i id='eYsRf'><tr id='eYsRf'><dt id='eYsRf'><q id='eYsRf'><span id='eYsRf'><b id='eYsRf'><form id='eYsRf'><ins id='eYsRf'></ins><ul id='eYsRf'></ul><sub id='eYsRf'></sub></form><legend id='eYsRf'></legend><bdo id='eYsRf'><pre id='eYsRf'><center id='eYsRf'></center></pre></bdo></b><th id='eYsRf'></th></span></q></dt></tr></i><div id='eYsRf'><tfoot id='eYsRf'></tfoot><dl id='eYsRf'><fieldset id='eYsRf'></fieldset></dl></div>
                  1. <small id='eYsRf'></small><noframes id='eYsRf'>

                    1. <tfoot id='eYsRf'></tfoot>
                    2. <legend id='eYsRf'><style id='eYsRf'><dir id='eYsRf'><q id='eYsRf'></q></dir></style></legend>