From: Kai T. <kti...@go...> - 2009-10-25 16:29:46
|
Hello David, 2009/10/25 David Cleaver <wr...@mo...>: > Hello, > > I'm having problems printing out values that are larger than 32 bits. My usual > printf using %llu only outputs the lower 32 bits of my 64-bit numbers. > > Here's an example program that produces incorrect output. > > #include <stdio.h> > > typedef unsigned long long u64_t; > > int main(int argc, char* argv[]) > { > u64_t y=6981463658333LL; > printf("y=%llu\n", y); > printf("y=%PRIu64\n", y); > printf("y=%qd\n", y); > printf("y=%x\n", y); > return 0; > } > > Here is the output I get. > y=2141802333 > y=PRIu64 > y=qd > y=7fa94f5d > > Can anyone see what I'm doing wrong? What is the proper way to output 64-bit > numbers? I'm using mingw-w64-bin_i686-mingw_20091025.zip for my development > environment and I've called the compiler like: > x86_64-w64-mingw32-gcc.exe -o test.exe test.c > > Thanks for any help. > > -David C. The reason for this is the msvcrt you are using. The %ll width specifier is support by MS until msvcrt based on msvcr80.dll. The common width specifier, which works also for older msvcrt.dll versions, is %I64. If you want to use POSIX like printf formatters, like "%ll" or "%Lg", then you should use our POSIX emulation version for it. You need just to define by command line, or before including the first header file, the macro __USE_MINGW_ANSI_STDIO with value one. For the example you gave it would look like that #define __USE_MINGW_ANSI_STDIO 1 #include <stdio.h> typedef unsigned long long u64_t; int main(int argc, char* argv[]) { u64_t y=6981463658333LL; printf("y=%llu\n", y); printf("y=%PRIu64\n", y); printf("y=%qd\n", y); printf("y=%x\n", y); return 0; } I hope I could help you by this. Cheers, Kai -- | (\_/) This is Bunny. Copy and paste | (='.'=) Bunny into your signature to help | (")_(") him gain world domination |