Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf and %llx in GCC under Windows 64x

Tags:

c

gcc

I am trying to get rid of a bogus warning in my program. Under windows 64 (under linux there is no such warning) this statement:

printf("%llu",UINT64_MAX);

generates the following warning:

warning: unknown conversion type character 'l' in format [-Wformat]|

The output appears to be correct and the warning should not be there. The most relevant gcc related post I can find is this bug report back from 2008 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37768

If I understand correctly according to that, this warning stems from the fact that under the hood gcc calls MSVC printf which is not C99 compliant and can't understand the unsigned long long format in printf. From the same page the suggested solution is to use something called gnu_printf. I tried to google that but I did not find a header to include.

So the question is how should this be handled in order to be portable? I just want to get rid of the warning in a correct and non-sloppy way.

like image 470
Lefteris Avatar asked May 26 '12 05:05

Lefteris


2 Answers

As I'm guessing you probably already know, from http://comments.gmane.org/gmane.comp.gnu.mingw.w64.general/4670 (note: dead link; see the Internet Archive's copy),

the issue is that formatter-width specifier %ll isn't supported for all msvcrt-DLL versions, therefore gcc warns about its use. The variant for specifying 64-bit integer-scalar-width in formatter for msvcrt in a backward-compatible way is by using %I64.

like image 174
paulsm4 Avatar answered Sep 27 '22 21:09

paulsm4


With gcc 6.2.1 you can use -fno-builtin option.

That option makes gcc think that printf isn't built-in (which is the case, after all), and the warning goes away, printf is treated just like any function you'd have written.

Careful though, as no format check is performed anymore in that case, and there are other side-effects as there are other built-ins covered by that option.

A better alternative is not to use Microsoft version of printf at all by setting the macro __USE_MINGW_ANSI_STDIO with:

gcc -D__USE_MINGW_ANSI_STDIO=1 ...

(ref: C program shows %zu after conversion to Windows). It allows to use %zu specifier, that Microsoft printf doesn't support and you keep formatting warnings.

like image 36
Jean-François Fabre Avatar answered Sep 27 '22 21:09

Jean-François Fabre