C中大写和小写双(浮点)类型说明符之间的区别
Difference between upper and lower case double (float) type specifiers in C
在Cdouble/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
之间最短。
在Cdouble/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
之间最短。