简体   繁体   English

在printf上输出

[英]output on the printf

is there anyone who can give me a vital and simple explanation im trying to understand this program below its about the printf in c programming here is the code...... 有谁能给我一个重要而简单的解释,我试图在其下面了解有关c编程中printf的程序,这里是代码……

#include <stdio.h>
  int main()
   {
    int a,b;
    float c;
      a = 1;
      b = 2;
      c = 3.;

    printf("%d %d %f\n",a ,b , c);
    printf("%d %f %f\n",a ,b , c);
    printf("%f %d %f\n",a ,b , c);
    printf("%d %d %d\n",a ,b , c);
    printf("%f %d %d\n",a ,b , c);
   return o;

    }

the output is 输出是

1  2  3.000000
1  0.000000  0.000000
0.000000  0  0.000000
1  2  0
0.000000  0  1074266112

im perfectly clear about the first printf's output but why does the second printf outputs 1 0.000000 0.000000 我完全清楚第一个printf的输出,但是为什么第二个printf输出1 0.000000 0.000000

instead of 1 0.000000 3.000000 而不是1 0.000000 3.000000

third printf 0.000000 0 3.000000 第三次0.000000 0 3.000000

fifth printf 0.000000 2 0.000000 第五次打印0.000000 2 0.000000

The behaviour of all the printf() statements except the first one is undefined . 除第一个语句外,所有printf()语句的行为均为undefined You therefore cannot expect them to behave in any particular way. 因此,您不能期望它们以任何特定方式运行。

To fix this, you need to cast the numeric arguments to printf() so that their types match the format specifier. 要解决此问题,您需要将数字参数转换为printf()以使它们的类型与格式说明符匹配。 The compiler does not perform this conversion automatically, although some good compilers would issue a warning to alert you to the mistake. 编译器不会自动执行此转换,尽管某些优秀的编译器会发出警告以警告您该错误。

When you pass an int to printf() and tell it to treat it as a float , you get the garbage out corresponding to the garbage in (GIGO). 当将int传递给printf()并告诉它将其视为float ,您得到的垃圾与(GIGO)中的垃圾相对应。 Basically, neither printf() nor the compiler can do any conversions for you. 基本上, printf()和编译器都无法为您进行任何转换。 You have to get it right. 您必须正确处理。 You must supply arguments that match the format specification. 您必须提供与格式规范匹配的参数。 These comments apply to all the printf() and scanf() families of functions. 这些注释适用于所有printf()scanf()函数系列。

If you use GCC and string literals as the format strings (as in your example), you will get compiler warnings about type mismatches. 如果将GCC和字符串文字用作格式字符串(如您的示例中所示),则会收到有关类型不匹配的编译器警告。 Pay heed and fix them; 注意并修复它们; the compiler knows more than you do. 编译器比您了解的更多。 If you're using a different compiler that does not give such warnings, find one (such as GCC) that does - and use it. 如果您使用的是另一种不发出此类警告的编译器,请找到一个发出该警告的程序(例如GCC)并使用它。 And make sure you turn the warnings on - and that your code compiles without emitting any warnings. 并确保打开警告-并且代码在编译时不会发出任何警告。

You are invoking 'undefined behaviour' with the second and subsequent statements, which means any and all answers (and other behaviours, including crashes and reformatting your disk) are acceptable responses by the system. 您正在使用第二条及其后的语句调用“未定义的行为”,这意味着系统可以接受任何和所有答案(以及其他行为,包括崩溃和重新格式化磁盘)。

You're running into undefined behavior . 您正在遇到未定义的行为 The runtime expects a float and you give it an int . 运行时需要一个float而您给它一个int

To expand above answers, and give a clue as to what is causing the undefined behavior. 扩展以上答案,并提供有关导致未定义行为的原因的线索。 On your platform, floats have a different size than ints, so printf() is wrong not only about the type of its arguments, but also about where they are in memory. 在您的平台上,浮点数的大小与ints的大小不同,因此printf()不仅在其参数类型方面而且在内存中的位置方面都是错误的。 So in the second line, printf is not looking at the start of the float when it prints the third value; 因此,在第二行中,printf在打印第三个值时未查看浮点数的开始; it is looking somewhere else. 它正在寻找其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM