Today I decided to recap a bit the C programming language fundamentals and I have encountered this small issue within my Code::Blocks IDE: when I had used the %f format identifier to read and write a decimal number everything went well, but when I had switched to the %lf format identifier, it did not read or write the number properly.
This is my code:
#include <stdio.h>
int main()
{
double x;
scanf("%lf", &x);
printf("x = %lf", x);
return 0;
}

This is the output:

These are the compiler settings from the Code::Blocks menu:

I have searched for a solution online, including the Code::Blocks forums, but I haven't found anything relevant. I am not sure whether is a compiler problem or an IDE problem. If you know a fix or you have an explanation for this issue, please help me. I am preety sure other people encountered this as well.
It is unclear what Code::Blocks is warning about. The lf in the scanf format string is also underlined but is definitely correct as the destination variable has type double.
For printf, the l modifier is unnecessary but should be ignored by printf. float values are converted to double when passed to vararg functions such as printf, so %f accepts both float and double values, while long double values require an L modifier.
The suggested corrections seem to indicate that Code::Block tries to apply some sort of spelling checker to string constant contents, regardless of conversion specifiers in printf and scanf format strings.
I suggest you change the printf format string to printf("x = %f\n", x); for full conformity.
You should also configure the compiler for higher warning levels (-Wall -W or -Weverything) to enable printf and scanf format string validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With