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 | jiangly | 3976 |
2 | tourist | 3815 |
3 | jqdai0815 | 3682 |
4 | ksun48 | 3614 |
5 | orzdevinwang | 3526 |
6 | ecnerwala | 3514 |
7 | Benq | 3482 |
8 | hos.lyric | 3382 |
9 | gamegame | 3374 |
10 | heuristica | 3357 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | -is-this-fft- | 165 |
3 | Um_nik | 161 |
4 | atcoder_official | 160 |
5 | djm03178 | 157 |
6 | Dominater069 | 156 |
7 | adamant | 154 |
8 | luogu_official | 152 |
9 | awoo | 151 |
10 | TheScrasse | 147 |
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
.