In C double/float has a set type specifiers: %f
%F
%g
%G
%e
%E
. Is there any difference between
%f
and %F
,%g
and %G
,%e
and %E
?According to printf
and scanf
the output is equal. Then why both upper and lower cases are valid?
Note that type specifiers for scanf double are starting with lowercase l
The difference between %f
and %F
are if they print infinity and NaN as lower case or upper case. Here is an example:
#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
and %F
are identical when printing real numbers. For example, printf("%f", 1.0)
and printf("%F", 1.0)
do exactly the same thing.
Note that %F
is only available in C99 or C++.
The difference between %e
and %E
is if the "e" that separates the number and the exponent is lower or upper case (for example 1.0e+0
or 1.0E+0
. It also makes a difference for infinity and Nan just like for %f
and %F
. Here is an example:
#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;
}
The difference between %g
and %G
is that %g
takes the shortest between %e
and %f
, and %G
takes the shortest between %E
and %F
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With