具有整数成员的对象的Printf

时间:2019-07-09 17:30:24

标签: c++ printf

我有这些奇怪的行为。 如果我使用下面的代码

<service_orders>
  <service_order>
    <account_name>MADISON BISTRO</account_name>
    <order_number>3238204</order_number>
    <delivery_type>Delivery</delivery_type>
    <customer_code>     10309</customer_code>
    <delivery_date>07/09/19</delivery_date>
    <cod> </cod>
    <note></note>
    <customer>
      <number>     10309</number>
      <delivery_type>Delivery</delivery_type>
      <customer_name>MADISON BISTRO</customer_name>
      <address1>135 N. COURT STREET</address1>
      <city>WAMPSVILLE</city>
      <state>NY</state>
      <zip>13163</zip>
      <phone1>3152800228</phone1>
      <open_time>900</open_time>
      <close_time>2000</close_time>
      <window_start_time_1>900</window_start_time_1>
      <window_end_time_1>1300</window_end_time_1>
      <latitude>43.0805</latitude>
      <longitude>-75.7077</longitude>
      <category_1_size_1> </category_1_size_1>
      <category_1_size_2> </category_1_size_2>
      <category_1_size_3> </category_1_size_3>
      <category_2_size_1> </category_2_size_1>
      <category_2_size_2> </category_2_size_2>
      <category_2_size_3> </category_2_size_3>
      <category_3_size_1> </category_3_size_1>
      <category_3_size_2> </category_3_size_2>
      <category_3_size_3> </category_3_size_3>
    </customer>
    <line_items>
        <line_item>
        <serial_number>     13075</serial_number>
        <quantity>1</quantity>
        <amount>81.57</amount>
        <description>BACON E.Z. PAN EXTRA SMOKE 10-12/LB</description>
        <line_taxes> </line_taxes>
        <category_name>Frozen</category_name>
        <size_1>.84</size_1>
        <size_2>21.00</size_2>
        <size_3>1.00</size_3>
    </line_item>
    <line_item>
        <serial_number>     22027</serial_number>
        <quantity>1</quantity>
        <amount>43.73</amount>
        <description>CORNED BEEF ROUND COOKED</description>
        <line_taxes> </line_taxes>
        <category_name>Refer</category_name>
        <size_1>.35</size_1>
        <size_2>12.82</size_2>
        <size_3>1.00</size_3>
    </line_item>
    <line_item>
        <serial_number>     25052</serial_number>
        <quantity>1</quantity>
        <amount>32.70</amount>
        <description>CHEESE CHEDDAR SHARP WHITE BLOCK</description>
        <line_taxes> </line_taxes>
        <category_name>Refer</category_name>
        <size_1>.23</size_1>
        <size_2>10.22</size_2>
        <size_3>1.00</size_3>
    </line_item>
    </line_items>
  </service_order>
  <service_order>
    <account_name>OLD SCHOOL BAR AND GRILL</account_name>
    <order_number>3238217</order_number>
    <delivery_type>Delivery</delivery_type>
    <customer_code>     13093</customer_code>
    <delivery_date>07/09/19</delivery_date>
    <cod> </cod>
    <note></note>
    <customer>
      <number>     13093</number>
      <delivery_type>Delivery</delivery_type>
      <customer_name>OLD SCHOOL BAR AND GRILL</customer_name>
      <address1>600 CULVER AVE SUITE 3</address1>
      <city>UTICA</city>
      <state>NY</state>
      <zip>13501</zip>
      <phone1>3157971980</phone1>
      <open_time>830</open_time>
      <close_time>2100</close_time>
      <window_start_time_1>900</window_start_time_1>
      <window_end_time_1>1300</window_end_time_1>
      <latitude>43.0902</latitude>
      <longitude>-75.1987</longitude>
      <category_1_size_1> </category_1_size_1>
      <category_1_size_2> </category_1_size_2>
      <category_1_size_3> </category_1_size_3>
      <category_2_size_1> </category_2_size_1>
      <category_2_size_2> </category_2_size_2>
      <category_2_size_3> </category_2_size_3>
      <category_3_size_1> </category_3_size_1>
      <category_3_size_2> </category_3_size_2>
      <category_3_size_3> </category_3_size_3>
    </customer>
    <line_items>
        <line_item>
        <serial_number>     12123</serial_number>
        <quantity>4</quantity>
        <amount>220.91</amount>
        <description>BEEF BUTT TENDERS FROZEN</description>
        <line_taxes> </line_taxes>
        <category_name>Frozen</category_name>
        <size_1>3.44</size_1>
        <size_2>141.64</size_2>
        <size_3>4.00</size_3>
    </line_item>
    <line_item>
        <serial_number>     88184</serial_number>
        <quantity>1</quantity>
        <amount>58.90</amount>
        <description>PEPPERS LONG HOT</description>
        <line_taxes> </line_taxes>
        <category_name>Refer</category_name>
        <size_1>1.39</size_1>
        <size_2>21.75</size_2>
        <size_3>1.00</size_3>
    </line_item>
    <line_item>
        <serial_number>     88137</serial_number>
        <quantity>10</quantity>
        <amount>22.81</amount>
        <description>LETTUCE ROMAINE CHOPPED</description>
        <line_taxes> </line_taxes>
        <category_name>Refer</category_name>
        <size_1>10.32</size_1>
        <size_2>130.00</size_2>
        <size_3>10.00</size_3>
    </line_item>
    </line_items>
  </service_order>
</service_orders>

由于printf,我得到数字“ 3”。 例如,如果我首先输入声明,例如变量“ mm”而不是“ mmm”,则作为printf的结果是数字“ 2”。 如果我首先在声明中输入变量“ m”,则printf给我“ 1”。 似乎printf可以打印声明的第一个int变量。怎么可能?

1 个答案:

答案 0 :(得分:3)

printf是一个可变参数函数,这意味着它不知道如何检查第二个及后续参数的类型。因此,在典型的实现中,它信任您为其提供了%d所指示的正确类型的参数,即int。您的sim对象在内存中被三个ints打包在一起。给定printf%d尝试读取int并获取您在int中声明的第一个sim变量。

请注意,这是未定义的行为,您不应依赖它。