Re: [Mingw-w64-public] 64-bit printf question...

2009-10-25 Thread Kai Tietz
Hello David,

2009/10/25 David Cleaver wrai...@morpheus.net:
 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

--
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] 64-bit printf question...

2009-10-25 Thread Ozkan Sezer
On Sun, Oct 25, 2009 at 5:55 PM, David Cleaver wrai...@morpheus.net wrote:
 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);

%llu is not supported by printf in msvcrt.dll.  You can use
%I64u which is the one supported by ms. Or you can use
__mingw_printf(), instead, either explicitly, or indirectly by
defining __USE_MINGW_ANSI_STDIO as 1, for example
by adding -D__USE_MINGW_ANSI_STDIO=1 to your
CFLAGS, which will automaticly replace all printf family
of functions with their __mingw_ counterparts.


   printf(y=%PRIu64\n, y);

Wrong syntax.  First you must #include inttypes.h for
this. Then, the correct form should be like

printf(y=% PRIu64 \n, y);

   printf(y=%qd\n, y);

Same as %ll stuff.

   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.

Use -Wall (or -Wformat) to get warnings about what is
supported and what is not.  Hope these help.

--
Ozkan

--
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public