在C double / float中有一个集类型说明符:%f
%F
%g
%G
%e
%E
。两者之间有什么区别吗?
%f
和%F
,%g
和%G
,%e
和%E
?据printf
和scanf
说,输出是平等的。那么为什么大小写都有效?
请注意,scanf double的类型说明符以小写l
开头
specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
% A % followed by another % character will write a single % to the stream. %
%f
和%F
之间的区别在于它们是否打印无穷大和NaN为小写或大写。这是一个例子:
#include <stdio.h>
#include <math.h>
int main(){
printf("%f and %f\n", INFINITY, nan("0")); //Prints "inf and nan"
printf("%F and %F\n", INFINITY, nan("0")); //Prints "INF and NAN"
return 0;
}
打印实数时,%f
和%F
是相同的。例如,printf("%f", 1.0)
和printf("%F", 1.0)
完全一样。
请注意,%F
仅适用于C99或C ++。
%e
和%E
之间的区别在于,将数字和指数分开的“e”是低或大写(例如1.0e+0
或1.0E+0
。对于无穷大和Nan也有区别,就像%f
和%F
一样。这是一个例:
#include <stdio.h>
#include <math.h>
int main(){
printf("%e, %e and %e\n", INFINITY, nan("0"), 1.0); //Prints "inf, nan and 1.0000e+00"
printf("%E, %E and %E\n", INFINITY, nan("0"), 1.0); //Prints "INF, NAN and 1.0000E+00"
return 0;
}
%g
和%G
之间的差异是%g
在%e
和%f
之间最短,而%G
在%E
和%F
之间最短。