小编Lak*_*hay的帖子

为什么“printf()”的这种模棱两可的行为?

我故意强迫printf()打印celsiusint(使用%8d格式说明符)。我知道这是打印的原因0(在标题下Celsius Scale)。

但我只想知道为什么fahr0.0在整个表格中打印。

我用过 GCC 编译器。

这是摄氏度到华氏度转换的代码:

#define LOWER 0.0F
#define UPPER 300.0F
#define STEP 20.0F

#include <stdio.h>

void main() {
    float celsius, fahr;
    printf("*Conversion from Celsius to Fahrenheit*\n");
    printf("Celsius Scale \t   Fahrenheit Scale\n");
    for (celsius = LOWER; celsius <= UPPER; celsius = celsius + STEP) {
        fahr = (9.0f * celsius / 5.0f) + 32.0f; 
        printf("%8d\t\t%5.1f\n", celsius, fahr);
    }
}
Run Code Online (Sandbox Code Playgroud)

下表是上述代码的 …

c floating-point printf

4
推荐指数
1
解决办法
233
查看次数

标签 统计

c ×1

floating-point ×1

printf ×1