71

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);
}
user963241
  • 6,758
  • 19
  • 65
  • 93
  • 3
    Did you use the `-std=c99` flag when compiling? –  Nov 27 '12 at 18:33
  • I get no warnings: i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00) – MK. Nov 27 '12 at 18:34
  • @netcoder: I'm not sure how to look for the version. I'm running gcc on windows x86. – user963241 Nov 27 '12 at 18:42
  • Ah, it's gcc version 4.6.1. Thanks @effeffe. – user963241 Nov 27 '12 at 18:57
  • Which version of Windows are you using? – effeffe Nov 27 '12 at 21:20
  • Well, reading the accepted answer and since GCC changes report doesn't mention this issue, that could be the problem. I'd try anyway to update the compiler. – effeffe Nov 27 '12 at 21:29
  • @effeffe: Windows xp service pack 3. – user963241 Nov 27 '12 at 21:51
  • @H2CO3, @effeffe: I just found that I didn't have `-std=c99` flag set. I thought that it was globally set for all projects. The program that I have in example compiles without any warnings. I apologize for any trouble. – user963241 Nov 27 '12 at 23:51
  • possible duplicate of [MinGW GCC: "Unknown conversion type character 'h'" (snprintf)](http://stackoverflow.com/questions/10678124/mingw-gcc-unknown-conversion-type-character-h-snprintf) – legends2k Dec 30 '14 at 00:42

4 Answers4

92

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.

kolp
  • 13
  • 4
nos
  • 223,662
  • 58
  • 417
  • 506
  • 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. – Jack Nov 27 '12 at 18:59
  • 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? – user963241 Nov 27 '12 at 19:02
  • @user963241 Not on windows, afaik. – nos Nov 27 '12 at 19:04
  • Could we open a chat about this? – Ramy Al Zuhouri Nov 27 '12 at 19:49
  • @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 Nov 27 '12 at 20:58
  • 3
    Warning: do not confuse the uppercase "i" in "%I64d" with lowercase L. I just did that mistake. – Adayah Jul 29 '15 at 18:02
  • 2
    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. – Shahbaz Nov 16 '16 at 00:54
  • 3
    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. – raymai97 Dec 18 '16 at 03:52
7

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);
bkdm
  • 308
  • 4
  • 5
  • 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. – Pryftan Feb 11 '22 at 15:08
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.

effeffe
  • 2,821
  • 3
  • 21
  • 44
  • Can you please add a link to the changes log for GCC 4.7 version? – user963241 Nov 27 '12 at 21:00
  • @user963241: hmm, can't find anything... http://gcc.gnu.org/gcc-4.7/ maybe it's about MinGW, but I can't access their site at the moment. – effeffe Nov 27 '12 at 21:11
  • 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. – user963241 Nov 27 '12 at 21:42
  • I have it, so yes. Maybe you're still using the old version someway. – effeffe Nov 27 '12 at 21:59
  • 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. – user963241 Nov 27 '12 at 22:01
-2

You can try settings of code::block, there is a complier..., then you select in C mode.

enter image description here

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 1
    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 Feb 11 '22 at 15:04