printf在打印浮点值时崩溃

Atu*_*tul 3 c c++ printf

printf()今天我遇到了一个奇怪的问题.但即使经过分析,我也找不到它的答案.因此在这里分享.我尝试了这三个printf()陈述:

printf("\nValue of this division is %f", (double)873/(double)65);
Run Code Online (Sandbox Code Playgroud)

它按预期打印正确的输出.

printf("\nSome message with an integer here %d followed by floats %f, %f, %f", 2013, 987/432, 873/65, 983/81);
Run Code Online (Sandbox Code Playgroud)

给我错误的值(因为我没有把它们加倍?)

printf("\nSome message with an integer here %d followed by floats %f, %f, %f and now string at end: %s", 2013, 987/432, 873/65, 983/81, "Some trial string here");
Run Code Online (Sandbox Code Playgroud)

printf()在这里坠毁了!这引起了两个问题:

  1. 我在MSDN中看到了"FormatOutput(LPCSTR formatstring,...)"示例,其中它们分配了固定宽度的目标缓冲区,然后vsnprintf()使用它进行调用.我相信printf()在同一条线上工作.但我没有找到任何内部缓冲区大小printf().如果它动态分配内存,那么它如何计算缓冲区大小?

  2. printf()在上面崩溃,因为vsnprintf()那里也崩溃了(是的,我尝试了上面参数FormatOutput给出的示例代码vsnprintf()).但为什么最终会崩溃呢?

Die*_*ühl 6

你答应打印doubles但是你传递的int是:987/432是的int.例如,如果您希望此值为double您使用的值987.0/432.