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 | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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
.