Printf long long int in C with GCC?

183,223

Solution 1

If you are on windows and using mingw, gcc uses the win32 runtime, where printf needs %I64d for a 64 bit integer. (and %I64u for an unsinged 64 bit integer)

For most other platforms you'd use %lld for printing a long long. (and %llu if it's unsigned). This is standarized in C99.

gcc doesn't come with a full C runtime, it defers to the platform it's running on - so the general case is that you need to consult the documentation for your particular platform - independent of gcc.

Solution 2

For portable code, the macros in inttypes.h may be used. They expand to the correct ones for the platform.

E.g. for 64 bit integer, the macro PRId64 can be used.

int64_t n = 7;
printf("n is %" PRId64 "\n", n);

Solution 3

Try to update your compiler, I'm using GCC 4.7 on Windows 7 Starter x86 with MinGW and it compiles fine with the same options both in C99 and C11.

Share:
183,223
user963241
Author by

user963241

Updated on July 05, 2022

Comments

  • user963241
    user963241 almost 2 years

    How do I printf long long int and also unsigned long long int in C99 using GCC?

    I have searched the other posts which suggest to use %lld but it gives these warnings:

    warning#1: unknown conversion type character 'l' in format [-Wformat]|
    warning#2: too many arguments for format [-Wformat-extra-args]|

    For the following attempt:

    #include <stdio.h>
    
    int main()
    {
       long long int x = 0;
       unsigned long long int y = 0;
       printf("%lld\n", x);
       printf("%llu\n", y);
    }
    
  • Jack
    Jack over 11 years
    I agree to you and moderators too, but the SO isn't really for this type of thread... by keeping your comment here may become chaos.
  • user963241
    user963241 over 11 years
    So, Is it a non-standard way to do it in windows? Any other compiler may I know which fully supports and follows standard rules for windows x86?
  • nos
    nos over 11 years
    @user963241 Not on windows, afaik.
  • Ramy Al Zuhouri
    Ramy Al Zuhouri over 11 years
    Could we open a chat about this?
  • user963241
    user963241 over 11 years
    @nos: As I know h modifies the length of decimal to short. Any idea about if hh works well in windows on gcc and what it does?
  • user963241
    user963241 over 11 years
    Can you please add a link to the changes log for GCC 4.7 version?
  • effeffe
    effeffe over 11 years
    @user963241: hmm, can't find anything... gcc.gnu.org/gcc-4.7 maybe it's about MinGW, but I can't access their site at the moment.
  • user963241
    user963241 over 11 years
    Did the latest GCC 4.7 compiler come with MinGW? I have just tried to download and install MinGW but it didn't update my compiler to version 4.7.
  • effeffe
    effeffe over 11 years
    I have it, so yes. Maybe you're still using the old version someway.
  • user963241
    user963241 over 11 years
    I think it could be that I'm using an old version of Windows and not the compiler. I'll try with latest compiler anyway.
  • Adayah
    Adayah almost 9 years
    Warning: do not confuse the uppercase "i" in "%I64d" with lowercase L. I just did that mistake.
  • Shahbaz
    Shahbaz over 7 years
    It may be worth noting that defining __USE_MINGW_ANSI_STDIO=1 will make mingw use a standard stdio. This is useful also because windows' printf doesn't understand %n.
  • raymai97
    raymai97 over 7 years
    Note that msvcrt of modern Windows (after Windows XP) does support %lld %llX. To get the program work consistently, you may use the method mentioned by @Shahbaz so you don't have to use that weird %I64d stuff.
  • Pryftan
    Pryftan about 2 years
    Just to give you reasons why your answer might have been voted down. It doesn't answer the question. It assumes the OP is using a specific tool. And maybe even more importantly it doesn't tell the OP how it's actually done - it just lets the tool do it for you. That's not good. Btw I didn't down-vote you as I know you tried but if someone is trying to learn how to do something telling them a tool that will do it for you is not going to teach them anything at all.
  • Pryftan
    Pryftan about 2 years
    As much as I hate those macros this should be the correct answer - though it came late. Btw int64_t is not guaranteed to exist. The _least versions are guaranteed. Perhaps you already know that though.