2

I'm encountering an issue while using the printf() function and I'd like to share the details. The two main sources I'm referring to, cplusplus.com and cppreference.com, indicate that I can use the %f and %F format specifiers.

However, when I attempt to use the %F format specifier in Code::Blocks 20.03, I'm unable to get the desired output. While this format specifier is indicated as usable in other sources, I'm puzzled as to why it's not working in my compiler.

code input:

#include<stdio.h>
#include<float.h>

int main() 
{
    float decimal_floating_point = 1.234567;

    float Decimal_Floating_Point = 123.4567;

    printf("decimal floating point = [%f]\n",decimal_floating_point);

    printf("Decimal Floating Point = [%F]\n",Decimal_Floating_Point);
}

code output:

decimal floating point = [1.234567]    
Decimal Floating Point = []

Why am I getting the result Decimal Floating Point = [] instead of the expected Decimal Floating Point = [123.4567]?

19
  • 1
    Which compiler is code::blocks configured to use, and how exactly is it called?
    – dbush
    Commented Apr 19, 2024 at 19:22
  • 2
    Release 20.03 rev 11983 (2020-03-12 18:24:30) gcc 8.1.0 Windows/unicode - 64 bit @dbush Commented Apr 19, 2024 at 19:32
  • 1
    Godbolt gcc v8.1 does not repro this behavior godbolt.org/z/MPrn5s9M6
    – Eugene Sh.
    Commented Apr 19, 2024 at 19:35
  • 1
    @WeatherVane: Blah. You're right. Ignore me.
    – John Bode
    Commented Apr 19, 2024 at 19:36
  • 1
    I suspect WSL funniness...
    – dbush
    Commented Apr 19, 2024 at 19:37

1 Answer 1

2

The %F format specifier was added to the C language by the 1999 ISO standard.

Apparently the implementation you're using includes a C runtime library whose printf function doesn't support %F.

In this case, %f and %F will produce exactly the same output. The output differs only for infinities and NaNs; %F produces INF, INFINITY, or NAN instead of inf, infinity, or nan, respectively.

Note that the version of your compiler doesn't affect this. The compiler (gcc in your case) will just generate a call to the printf function. It's the runtime library that implements that function. The compiler and the runtime library are components of a C implementation.

It's not uncommon for C implementations for Microsoft Windows to combine a reasonably modern compiler like gcc with an old version of Microsoft's C runtime library.

(Incidentally, the identifiers decimal_floating_point and Decimal_Floating_Point are misleading. Floating-point values are almost certainly stored using binary floating-point, typically IEEE. Decimal floating-point is permitted but rare.)

3
  • "Apparently the implementation you're using includes a C runtime library whose printf function doesn't support %F" do you have a reference? Apparently gcc 8.1.0 supports even C11. Commented Apr 19, 2024 at 20:11
  • 2
    @ChukwujiobiCanon You missed the "Note that the version of your compiler doesn't affect this. The compiler (gcc in your case) will just generate a call to the printf function. It's the runtime library that implements that function" part.
    – Eugene Sh.
    Commented Apr 19, 2024 at 20:16
  • 1
    Yea I missed that. Thanks. Commented Apr 19, 2024 at 20:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.