C99 Types and printf: faking it

C99 Types and printf: faking it

Post by shado » Tue, 04 Jun 2002 23:59:30



I've run into a problem with systems that don't have proper support
for C99 standard int types (int32_t, uint16_t, ... etc).

Specifically this FreeBSD 4.5-STABLE machine defines the standard
types in inttypes.h as it should but I cannot find any conversion
specifiers for printf() to print these values safely.

There are no PRI* macros, and their printf does not support the %j
conversion. The FreeBSD C99 project page is not very encouraging :|

Is there anyway to safely print out standard int values, and get
the code to compile everywhere without warnings (or the famous death
rattle), on FreeBSD or systems like it?

I'm considering using a third-party stdio printf replacement like
Trio (http://www.sourceforge.net/projects/trio.h).

--
Thamer Al-Harbash    http://www.whitefang.com/dhcp-agent/
"Yum! Yum! It's DHCPelicious!" -- wandering UDP elf
                                 comments on dhcp-agent.

 
 
 

C99 Types and printf: faking it

Post by Bjorn Rees » Wed, 05 Jun 2002 03:22:48



> Is there anyway to safely print out standard int values, and get
> the code to compile everywhere without warnings (or the famous death
> rattle), on FreeBSD or systems like it?

Casting the values to 'long int' and printing them with %ld is probably
the closest to a portable solution you will get. If you don't mind
fiddling with autoconf, you can check for the existance of the missing
macros, then for %j and intmax_t, and finally for %lld and 'long long
int', and use the best suited alternative.

Quote:> I'm considering using a third-party stdio printf replacement like
> Trio (http://www.sourceforge.net/projects/trio.h).

Slight correction, the URL is

  http://www.sourceforge.net/projects/ctrio/

Trio does not contain the missing PRI* macros, but it does have several
alternatives.

 1. It supports %j.

 2. It supports the MS I8, I16, and I32 flags. Numbers can be printed
    with

    trio_printf("%I32d", your_int32_t_number);

 3. It contains an undocumented flag for letting the application specify
    the size of the integers. This flag can be used like this

    trio_printf("%&d", sizeof(your_number), your_number);

    The reason this flag is left undocumented is because it assumes some
    uniform relationship between the size and the layout of the various
    types of integers. In theory it may be possible to have little-endian
    ints and big-endian longs, which both are 32 bits wide. On such hardware
    this flag will not work correctly -- not that I've ever heard of any.