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

时间:2016-01-10 13:57:15

标签: c types double format-specifiers

在C double / float中有一个设置类型说明符:%f %F %g %G %e %E

之间有什么区别吗?
  • %f%F
  • %g%G
  • %e%E

根据printfscanf,输出相等。那么为什么大写和小写都有效?

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

开头

2 个答案:

答案 0 :(得分: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之间的区别在于&#34; e&#34;将数字和指数分开的是大写或小写(例如1.0e+01.0E+0。对于无穷大和楠也有区别,就像%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之间的距离最短var allowedLetters = ["A","B","C","D","E","F","G","H"]; var i = Math.round(Math.random() * (allowedLetters.length - 1)); var letter = allowedLetters[i]; console.log(letter) private String userID; private FirebaseDatabase mFirebaseDatabase; private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener mAuthListener; private DatabaseReference myRef; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //Authenticate user mAuth = FirebaseAuth.getInstance(); mFirebaseDatabase = FirebaseDatabase.getInstance(); myRef = mFirebaseDatabase.getReference(); FirebaseUser user = mAuth.getCurrentUser(); if(mAuth.getCurrentUser() == null){ finish(); startActivity(new Intent(this, MainActivity.class)); } userID = user.getUid(); public void onDataChange(DataSnapshot dataSnapshot) { ArrayList<CharSequence> array = new ArrayList<>(Arrays.asList(HomeActivity.this.getResources().getTextArray(R.array.array))); map = (Map<String, Object>) dataSnapshot.child("users").child(userID).getValue(); int x = 16; for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); array.add(x, key); x++; }

答案 1 :(得分: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