Reputation: 1359
I have the following code:
#include <cstdio>
int main()
{
float b = 323.23f;
std::scanf("%6.3f\n", &b); // <-- Warning
std::printf("%6.3f\n", b);
}
There is a warning at scanf()
stating:
Invalid conversion specifier
'.'
Is there something I am missing here?
Upvotes: 2
Views: 417
Reputation: 386038
When you use %6.3f
with printf
, the .3
specifies the precision of the output.
scanf
doesn't need you to specify the precision, because it will accept whatever precision it finds in the input stream. So you aren't allowed to specify .3
(or .
-anything-else) in a scanf
format. Just use %6f
or even %f
instead.
Upvotes: 8
Reputation: 31
The format specifier in C does not take the value '6.3'.You can use '%6f' or simply '%f'. '%6.3f' is the thing that is causing the warning.
Upvotes: 1
Reputation: 3066
The format specifier for scanf
doesn't require you to explicitly specify the number of places after the decimal point. Plus, it is what is causing the warning, it is better off to use just a plain %f
inside the scanf
though a %6f
also works.
This isn't the case with the printf
though. When you use a [some_number].[number_of_places]f
, like, for example, 6.3f
in a printf
, while the .3f
would affect the number of places after the decimal point which would be displayed, the 6
in it reserves that many character spaces to display the entire number.
Hope this helps to some extent.
Upvotes: 1