printing uin32_t in hexadecimal

M

Maxime Barbeau

Hi.

I have the following code:
uint32_t quadlet = 0x12345678;
printf("quadlet = %08X\n", quadlet);

my compiler give me the following warning: unsigned int format, uint32_t
arg (arg 2)

Is there any formtat I can use? %08uX ?

thanks

Maxime
 
T

Tom St Denis

Maxime said:
Hi.

I have the following code:
uint32_t quadlet = 0x12345678;
printf("quadlet = %08X\n", quadlet);

my compiler give me the following warning: unsigned int format, uint32_t
arg (arg 2)

Is there any formtat I can use? %08uX ?

%lx

Tom
 
C

Chris


No, %lx is not completely correct. %lx expects an unsigned long.
Although unsigned long and uint32_t have the same representation on many
systems, there's absolutely no requirement for them to. We do know that
unsigned long is at least 32 bits, so a more proper method would be to
use %lx and cast the argument to unsigned long. That way, if unsigned
long is, say, 64 bits, printf() won't grab an extra 32 bits of junk off
the stack (or whatever method your implementation happens to use for
argument passing).

Chris
 
C

CBFalconer

Maxime said:
I have the following code:
uint32_t quadlet = 0x12345678;
printf("quadlet = %08X\n", quadlet);

my compiler give me the following warning: unsigned int format,
uint32_t arg (arg 2)

Is there any formtat I can use? %08uX ?

Using uint32_t requires #include <stdint.h> and a C99 compiler
system. You probably don't have either.
 
K

Keith Thompson

Maxime Barbeau said:
I have the following code:
uint32_t quadlet = 0x12345678;
printf("quadlet = %08X\n", quadlet);

my compiler give me the following warning: unsigned int format, uint32_t
arg (arg 2)

Is there any formtat I can use? %08uX ?

Where is uint32_t defined? If you're using C99, uint32_t is a typedef
in <stdint.h>; it's probably equivalent to one of unsigned short,
unsigned int, or unsigned long (most likely the latter in your case).
But you don't know which, so you can't safely use a format for one of
the predefined types.

The C99 header <inttypes.h> provides macros that expand to printf
formats for various types, including uint32_t, but they're ugly.

But you know that uint32_t is exactly 32 bits, and you know that
unsigned long is at least 32 bits, so you can do this:

uint32_t quadlet = 0x12345678;
printf("quadlet = %08lX\n", (unsigned long)quadlet);
 
J

Jack Klein

Using uint32_t requires #include <stdint.h> and a C99 compiler
system. You probably don't have either.

That's just plain bullschildt, Chuck.

Using uint32_t requires a <stdint.h> header, or equivalent. Any
conforming C90 compiler that has the right size integer types with 2's
complement representation, and that is the overwhelming majority of
them, will work just fine with a properly written <stdint.h> file, for
whatever subset of C99 integer types that they support and they are
not hard at all to write. They're just typedefs, after all.

Every conforming C89/C90 implementation can support all of the
''least'' and ''fastest'' up to 32 bits, if not 64 bits.

And what conforming implementation have you ever actually used that
did not provide an exact-width 32-bit integer type with no trap
representations and using 2's complement for the signed version?

I've written several (subset) stdint.h headers for a variety of
embedded 16-bit, 32-bit, and DSPs. Heck, the one that comes with
ARM's ADS 1.1 or 1.2 development tools compiles and works perfectly
with Visual C++ 6.

The corresponding <inttypes.h> header with its macros for printf() and
scanf() is more work, but it is also possible and I've done it.

Many compilers, even though they do not support all of C99, include a
working <stdint.h> and <inttypes.h>, even quite a few available for
free download. All of the 32-bit ones I know of support the 64-bit
types as well. Here's a list of compilers that I know have these
headers:

-- Borland's free C++BuilderX

-- mingw

-- gcc

-- lcc-win32

-- Codewarrior for Windows

-- ARM ADS

-- IAR for ARM

....and I've made subset headers that worked on platforms like TI DSPs
and 16 micros. Heck, I could write subset headers in half an hour or
less for an 8051, if I couldn't get out of never using on again.

So come off your high horse, you do not need C99 for using and
printing uint32_t, all you need is a pair of headers that can be used
quite well in C89/C90, or in some K&R 1 implementations for that
matter. It requires no language features at all, just typedefs and
the automatic string concatenation for the <inttypes.h> macros that
wasn't universal until C89.
 
C

CBFalconer

Jack said:
That's just plain bullschildt, Chuck.

Using uint32_t requires a <stdint.h> header, or equivalent. Any
conforming C90 compiler that has the right size integer types with 2's
complement representation, and that is the overwhelming majority of
them, will work just fine with a properly written <stdint.h> file, for
whatever subset of C99 integer types that they support and they are
not hard at all to write. They're just typedefs, after all.

Am I wrong that stdint.h did not exist in C90? M. Barbeau
certainly did not show any definitions for that term. Isn't
uint_32 implementation optional even in C99?
 
K

Keith Thompson

CBFalconer said:
Am I wrong that stdint.h did not exist in C90? M. Barbeau
certainly did not show any definitions for that term. Isn't
uint_32 implementation optional even in C99?

uint32_t is mandatory in C99 if (and only if) the implementation
provides a 32-bit two's complement integer type with no padding bits.
(The standard's wording is a trifle ambiguous, but I think there's a
DR clarifying it.) But there are C90-compatible implementations of
<stdint.h>.

Note that the original poster got an error message for the printf
format but not for the declaration of quadlet, so there must have been
a declaration of uint32_t somewhere.
 
M

Maxime Barbeau

Hello every one.

Thanks for your help

I am writing a program that will go on an embedded system.
I am using VxWorks for x86 and the PPC family.
This code is shared with QNX and will probably go under other OSs and CPUs.

The #include <inttypes.h> or <stdint.h> is certainly included in the header
I use.

After reading the posts, I guess I can use printf("quadlet: %08lX\n",
quadlet);


Maxime
 
B

Ben Pfaff

Maxime Barbeau said:
I have the following code:
uint32_t quadlet = 0x12345678;
printf("quadlet = %08X\n", quadlet);

Actually, you want:
printf("quadlet = %"PRIx32"\n", quadlet);
assuming you have a working <inttypes.h>.
 
M

Maxime Barbeau

Thanks

Maxime

Ben Pfaff said:
Actually, you want:
printf("quadlet = %"PRIx32"\n", quadlet);
assuming you have a working <inttypes.h>.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top