gpt4 book ai didi

c - 将 %f、%e、%g、%E 或 %G 与 scanf 一起使用有什么区别吗?

转载 作者:太空狗 更新时间:2023-10-29 16:44:32 25 4
gpt4 key购买 nike

在 C 中,当使用 scanf 读入浮点变量时,格式说明符 %f、%e、%g、%E 和 %G 有什么不同吗?即代码片段的行为会不会

float x;
scanf("%<one of f, e, g, E, G>", &x);

曾经依赖于说明符的选择吗?

我首先假设 %f 只能正确解释十进制计数法,而 %e 只能正确解释科学计数法,但在我的系统上,它们在任何一种情况下都可以正常工作。但也许我的系统只是自由的......

我在我的书中或网络上找不到关于此的任何明确声明...

最佳答案

上面的答案是针对C++的,但是对于C也是一样。

来自"Final version of the C99 standard with corrigenda TC1, TC2, and TC3 included, formatted as a draft"中的“7.19.6.2 fscanf函数” (从 http://en.wikipedia.org/wiki/C99 复制的链接):

a,e,f,g
Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of the strtod function. The corresponding argument shall be a pointer to floating.

The conversion specifiers A, E, F, G, and X are also valid and behave the same as, respectively, a, e, f, g, and x.

所以 %f, %e, %g, %E, %G扫描数字时的行为完全相同,如您所见。

关于c - 将 %f、%e、%g、%E 或 %G 与 scanf 一起使用有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com