C语言 将 %f、%e、%g、%E 或 %G 与 scanf 一起使用有什么区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19894483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any difference in using %f, %e, %g, %E or %G with scanf?
提问by lee77
In C, is there any difference in the format specifiers %f, %e, %g, %E and %G when used to read into a float variable with scanf? That is, will the behaviour of the code snippet
在 C 中,当用于使用 scanf 读入浮点变量时,格式说明符 %f、%e、%g、%E 和 %G 有什么区别吗?也就是说,将代码片段的行为
float x;
scanf("%<one of f, e, g, E, G>", &x);
ever depend on the choice of the specifier?
曾经依赖于说明符的选择吗?
I first supposed that %f would only interpret decimal notation correctly, and %e would only interpret scientific notation correctly, but on my system, each of them works fine in either case. But maybe my system is just liberal...
我首先假设 %f 只会正确解释十进制记数法,而 %e 只会正确解释科学记数法,但在我的系统上,它们在任何一种情况下都可以正常工作。但也许我的系统只是自由的......
I couldn't find any definite statement about this in my books or on the web...
我在我的书或网上找不到任何关于这个的明确陈述......
采纳答案by Martin R
The above answer refers to C++, but the same is true for C.
上面的答案是针对C++的,但是对于C也是一样的。
From "7.19.6.2 The fscanf function" in the "Final version of the C99 standard with corrigenda TC1, TC2, and TC3 included, formatted as a draft"(link copied from http://en.wikipedia.org/wiki/C99):
来自“包含勘误 TC1、TC2 和 TC3 的 C99 标准的最终版本,格式为草稿”中的“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 Xare also valid and behave the same as, respectively, a, e, f, g,and x.
a,e,f,g
匹配可选的有符号浮点数、无穷大或 NaN,其格式与 strtod 函数的主题序列的预期格式相同。相应的参数应该是一个指向浮动的指针。转换说明符A、E、F、G和X也是有效的,其行为分别与a、e、f、g和x 相同。
So %f, %e, %g, %E, %Gall behave identically when scanningnumbers, as you experienced.
因此,正如您所经历的那样,%f, %e, %g, %E, %G在扫描数字时,所有行为都相同。
回答by Rahul Tripathi
f,e,gall are for Floating point number
f,e,g都是浮点数
From the doc:-
来自文档:-
A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod). Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
一系列十进制数字,可选包含小数点,可选前面有符号(+ 或 -),可选后跟 e 或 E 字符和十进制整数(或 strtod 支持的一些其他序列)。符合 C99 的实现也支持以 0x 或 0X 开头的十六进制浮点格式。
Also check this referencewhich says that it(f,e,g) matches a floating-point number.
还要检查这个引用它说 it( f,e,g) 匹配一个浮点数。
回答by Keshav Bansal
C displays both float and double variables to six decimal places. This does NOT refer to the precision (accuracy) of which the number is actually stored, only how many decimal places printf()uses to display these variable types.
C 将 float 和 double 变量显示到小数点后六位。这不是指实际存储数字的精度(准确度),而是指printf()用于显示这些变量类型的小数位数。
The following program illustrates how the different data types are declared and displayed:
以下程序说明了如何声明和显示不同的数据类型:
#include <stdio.h>
main()
{
float a = 23.567;
double b = 11e+23;
printf("Float variable is %f\n", a);
printf("Double variable is %e\n", b);
}
OUTPUT
输出
Float variable is 23.567000
Double variable is 11.000000e23
回答by Sameer Sharma
%f: It prints corresponding number as a decimal floating point number, e.g. 214.52
%f:将对应的数字打印为十进制浮点数,例如 214.52
%e: It prints the number in scientific notation, e.g. 214.52e+2.
%e:以科学记数法打印数字,例如 214.52e+2。
%g: It prints the corresponding number in the shortest among two, e.g. In the above two examples, if I want to print the number through %g then it would be 214.52 but not the 2.145e+2 as it is longer.
%g: 它打印两个中最短的相应数字,例如在上面的两个例子中,如果我想通过 %g 打印数字,那么它将是 214.52 而不是 2.145e+2 因为它更长。
#include<stdio.h>
void main()
{
float f=12.5;
printf("%f\n",f);
printf("%e\n",f);
printf("%g\n",f);
}
Output:
输出:
12.500000
12.500000
1.250000e+001
1.250000e+001
12.5
12.5
回答by hukeping
base on @Sameer Sharma answer
基于@Sameer Sharma 的回答
$ cat test.c
#include<stdio.h>
void main()
{
float f=12.5123;
printf("%f\n",f);
printf("%e\n",f);
printf("%g\n",f);
}
$ gcc test.c
$ ./a.out
12.512300
1.251230e+01
12.5123
回答by mohit
%i=defaults for decimals
%u=gives the memory address of the variable
%p=gives the value of pointer
%e=gives the scientific notation
%g=handles large floating numbers

