Printf long long int in C with GCC?
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.
user963241
Updated on July 05, 2022Comments
-
user963241 almost 2 years
How do I printf
long long int
and alsounsigned 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 over 11 yearsI 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 over 11 yearsSo, 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 over 11 years@user963241 Not on windows, afaik.
-
Ramy Al Zuhouri over 11 yearsCould we open a chat about this?
-
user963241 over 11 years@nos: As I know
h
modifies the length of decimal toshort
. Any idea about ifhh
works well in windows on gcc and what it does? -
user963241 over 11 yearsCan you please add a link to the changes log for GCC 4.7 version?
-
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 over 11 yearsDid 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 over 11 yearsI have it, so yes. Maybe you're still using the old version someway.
-
user963241 over 11 yearsI 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 almost 9 yearsWarning: do not confuse the uppercase "i" in "%I64d" with lowercase L. I just did that mistake.
-
Shahbaz over 7 yearsIt 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 over 7 yearsNote 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 about 2 yearsJust 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 about 2 yearsAs 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.