Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #57753

Re: printing members of a structure

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: printing members of a structure
Date 2015-02-27 08:20 -0800
Organization None to speak of
Message-ID <87twy75nsz.fsf@kst-u.example.com> (permalink)
References (1 earlier) <claajmFr6qaU1@mid.individual.net> <mcp9ep$vq$1@dont-email.me> <mcp9lu$g35$1@dont-email.me> <mcphds$vq$3@dont-email.me> <clb2h9F1vusU1@mid.uni-berlin.de>

Show all headers | View raw


jt@toerring.de (Jens Thoms Toerring) writes:
[...]
> If the structure element is (as it seems), 32 bit value and
> you want to make sure it gets printed correctly on all systems
> you either can cast the value to an unsigned long to make sure
> it gets converted to an unsigned long before it's being passed
> to printf()
>
>   printf("hw_available: %lu\n", (unsigned long) p->hw_available);
>
> (note: an unsigned long will always suffice for a 32 bit
> value since the standard requires that (unsigned) longs
> have at least 32 bits) or you could use the C99 format
> specifier macro defined in "inttyes.h":
[snip]

(Pedantic quibble: arrays have elements, structures have members.)

You probably didn't notice that hw_available is actually a bit field:

    Uint32 hw_available :1;

It can only have the values 0 and 1, so it's sufficient to convert to
unsigned and print using "%u":

    printf("hw_available: %u\n", (unsigned) p->hw_available);

A few more comments:

Support for bit fields of types other than int, signed int, unsigned
int, and _Bool (bool) is implementation-defined; the signedness of a
plain "int" bit field is implementation-defined.

Presumably Uint32 is a typedef for an unsigned 32-bit integer type.  In
modern C code, there's no good reason not to use uint32_t, defined in
<stdint.h>, for this purpose.  (There are some odd technicalities for
platforms with non-2's-complement signed integers, but they're unlikely
to apply.)  Perhaps Uint32 was defined for some pre-C99 implementatioon,
and kept for backward compatibility.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 03:42 +0000
  Re: printing members of a structure Ian Collins <ian-news@hotmail.com> - 2015-02-27 17:37 +1300
    Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 08:21 +0000
      Re: printing members of a structure Richard Heathfield <rjh@cpax.org.uk> - 2015-02-27 08:26 +0000
        Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 10:37 +0000
          Re: printing members of a structure Barry Schwarz <schwarzb@dqel.com> - 2015-02-27 03:16 -0800
            Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 12:52 +0000
          Re: printing members of a structure jt@toerring.de (Jens Thoms Toerring) - 2015-02-27 11:25 +0000
            Re: printing members of a structure jt@toerring.de (Jens Thoms Toerring) - 2015-02-27 11:31 +0000
              Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 22:58 +0000
            Re: printing members of a structure Keith Thompson <kst-u@mib.org> - 2015-02-27 08:20 -0800
              Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 22:55 +0000
  Re: printing members of a structure Barry Schwarz <schwarzb@dqel.com> - 2015-02-26 22:03 -0800
    Re: printing members of a structure Chad <cdalten@gmail.com> - 2015-02-26 23:06 -0800
    Re: printing members of a structure bilsch <king621@comcast.net> - 2015-02-27 08:23 +0000
      Re: printing members of a structure Nick Bowler <nbowler@draconx.ca> - 2015-03-02 18:40 +0000
        Re: printing members of a structure James Kuyper <jameskuyper@verizon.net> - 2015-03-02 14:28 -0500
          Re: printing members of a structure Nick Bowler <nbowler@draconx.ca> - 2015-03-03 15:07 +0000

csiph-web