C中大小写双(浮点)类型说明符的区别

问题描述 投票:2回答:2

在C double / float中有一个集类型说明符:%f %F %g %G %e %E。两者之间有什么区别吗?

  • %f%F
  • %g%G
  • %e%E

printfscanf说,输出是平等的。那么为什么大小写都有效?

请注意,scanf double的类型说明符以小写l开头

c types double format-specifiers
2个回答
0
投票
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.    %

The Source of above


2
投票

%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+01.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之间最短。

© www.soinside.com 2019 - 2024. All rights reserved.