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?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 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?
Название |
---|
%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.
Usually it's enough to print float, You may cat double to float and use
%f
, which usually works fineTraditionally, 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
.There is slow, but simple method: