3

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;
}

enter image description here

This is the output:
enter image description here

These are the compiler settings from the Code::Blocks menu: enter image description here

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.

  • 1
    You have asked your question clearly but please post the code as text and not as an image. That will make it easier for others to copy to try out or comment on. – kaylum Dec 08 '19 at 10:58
  • 2
    Your configuration looks correct. `%lf` was supported (i.e. tolerated) as of C99, though it doesn't amount to much since `%f` properly prints `double` anyway (there is no `float` to `printf` ; `float becomes `double`). Which version of `gcc` are you using, and is there any chance you can see the actual build command emitted by codeblocks ? Fwiw, the later standards (C11) define behavior of the `l` modifier exclusively for conversions `d, i, o, u, x,` or `X` , and that it has no affect when preceding `f` in a printf, so I'm curious if behavior changes when you switch to C11. – WhozCraig Dec 08 '19 at 11:09
  • @kaylum Thanks for the tip! I've done this mainly to show what is highlighted when hovering over the identifier. – Revnic Robert-Nick Dec 08 '19 at 12:03
  • @WhozCraig I switched the compiler to C11, but the result was the same. When I unchecked the **-std=c99** option, everything went well (I assume it used C89). Anyway, the fix was to use `printf("x = %f", x);`. My assumption is that the value was read correctly, but the display format identifier was wrong, because printf would promote automatically the float to double if the gven parameter is indeed a double. – Revnic Robert-Nick Dec 08 '19 at 12:39
  • 1
    @RevnicRobert-Nick Does Code::Blocks still give that underline for the `scanf` after you've changed the `printf`? – Spikatrix Dec 08 '19 at 13:39
  • @Spikatrix Nope. And the suggestions don't appear either. – Revnic Robert-Nick Dec 08 '19 at 13:51

2 Answers2

2

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • If I use `printf("x = %f\n", x);`, the correct value is displayed. As an extra mention here: if I use the C89 standard, the output is correct, even if I use `%lf` inside the `printf` function. Meanwhile, I found that my issue was described in other posts as well: https://stackoverflow.com/questions/31600931/why-conversion-specification-lf-does-not-work-for-double-in-printf https://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f – Revnic Robert-Nick Dec 08 '19 at 12:55
0

The issue is described in more detail inside this thread for those who are interested:
Correct format specifier for double in printf