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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
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
.