I found that "lf" is ok for GNU C++ and MS C++, but not ok for C++ 0x using printf. Also, "f" works well for them.
float and double both use "f" ?
How about scanf ?
Could any body offer me some references?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
I found that "lf" is ok for GNU C++ and MS C++, but not ok for C++ 0x using printf. Also, "f" works well for them.
float and double both use "f" ?
How about scanf ?
Could any body offer me some references?
Name |
---|
%f
is for float only.%lf
is for double. It's both for scanf and printf.however, if you use lf for double in c++ 0x , it does not work.
If you use GCC, you can add
-D__USE_MINGW_ANSI_STDIO=0
parameter to compilation command line and%lf
will work quite OK.I guess problem is that you can't change compilation params on CF
To operate on
double
, use%f
or%lf
withprintf()
, but only%lf
withscanf()
. Reference: the C99 standard, 7.19.6.1.7, 7.19.6.1.8 and 7.19.6.2.11. C++11 is based on C99 (1.1.2) and includes its standard library (17.2, 27.9.2). So the same format must work for C++11, too.If it does not work, then your C++ implementation might not be fully conformant.
Traditionally, only
%f
can be used inprintf
for printingdouble
s andfloat
s. The main idea is that when passing any floating-point numbers (float
ordouble
) to variadic functions likeprintf
they are automatically promoted todouble
, so you don't have to (or can't) distinguishdouble
fromfloat
and only one format specifier is used inprintf
. But this confused some people, so the new version of C (but not the current version of C++) allows the use of%lf
fordouble
s inprintf
.