Historically, MinGW has been in a bit of an odd situation, especially as far as C99 support goes. MinGW relies mostly on the msvcrt.dll runtime that's distributed with Windows, and that runtime doesn't support C99.
So with older versions of MinGW, you can run into problems in C99 mode when using C99-specific format specifiers. Also historically, GCC didn't make any special accommodations for msvcrt.dll's lack of support for C99 specifiers. So you'd get into situations where -Wformat
wouldn't warn about a format that wouldn't work.
Things are improving on both sides - GCC has specific support for -Wformat when used with the MS runtime, such as:
-Wpedantic-ms-format
so that GCC won't complain about "I32"
and "I64"
(even though it's documented, I still get a complaint about it being unrecognized even in 4.7.0 - maybe it's brand new)
- the
ms_printf
option to __attribute__((__format__))
On the other side, MinGW has provided its own snprintf()
for a while, since MSVC's variant, _snprintf()
, behaves quite differently. However, MinGW relied for a long while on the printf()
in msvcrt.dll, so C99 format specifiers for printf()
didn't work. At some point MinGW started providing it's own version of printf()
and friends so that you could get proper C99 (and GNU?) support. However, it seems that to be on the conservative side, these didn't replace the msvcrt.dll versions initially. They have names like __mingw_printf()
.
It looks like at some point between 4.6.1 and 4.7.0, the MinGW headers started using the MinGW supplied versions as replacements for the msvcrt.dll function (at least if you've specifed C99).
However, it seems that with the newer versions, GCC and MinGW are still a little out of sync. Where as before GCC would not warn about specifiers that wouldn't actually work on MinGW, not it complains about spcifiers that will.
You may want to try the following snipet of code to see how well your version of MinGW support "hhX"
:
printf("%hhX\n", 0x11223344);
__mingw_printf("%hhX\n", 0x11223344);
I'm not sure what to suggest to fix the problem you're running into - I think that you may be able to patch the MinGW stdio.h
header so that it has a __attribute__((__format__ (gnu_printf, ...)))
attribute on the printf functions (they're not there in the newer stdio.h
, so GCC will use it's default idea of what the format support is).
printf()
and thesnprintf()
- what MinGW distro are you using? I'm currently using the TDM distribution. – Rafaelllesnprintf()
doesn't seem to handle"%2hhX"
as I expect - it ignores the additionalh
so if you pass in a negative number or an unsigned with a value larger than 255 you'll get more than two characters printed out. I think that technically that's undefined behavior, but still if you're expecting only the least significant byte to be printed, some further test might be in order. 4.7.0 works as expected, I don't have a 4.6.2 to test. – Rafaellle