linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn.clements@virgin.net>
To: Holger Kiehl <Holger.Kiehl@dwd.de>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Howto print off_t
Date: Thu, 27 Jun 2002 22:18:36 +0100	[thread overview]
Message-ID: <15643.33068.680130.50185@cerise.nosuchdomain.co.uk> (raw)
In-Reply-To: <Pine.LNX.4.33.0206272049120.31948-100000@talentix.dwd.de>


Holger Kiehl wrote:

> > > One could code this as follows:
> > >
> > >     if (sizeof(off_t) == 4)
> > >        printf("%ld\n", off_t_var);
> > >     else
> > >        printf("%lld\n", off_t_var);
> >
> > It's probably better to use the preprocessor, i.e.
> >
> > 	#if sizeof(off_t) == 4
> >
> Does the sizeof operator work in the preprocessor? I tried this but it
> does not seem to work for me.

No, sorry; I'd forgotten about the "types" exception for #if.

The argument to #if is a "constant expression", with the same
definition as in C itself (i.e. what can occur on the RHS of a static
initialiser), *EXCEPT* that it can't use "sizeof", a cast, or an enum
constant, but can use "defined()".

If you're using autoconf, you can use AC_CHECK_SIZEOF, e.g.

	AC_CHECK_SIZEOF(off_t)

will define SIZEOF_OFF_T to the appropriate value, so you can then
use e.g.

	#include <config.h>
	...
	#if SIZEOF_OFF_T == 4
	...

> > > But is this portable?
> >
> > No. This should be portable:
> >
> > 	#if sizeof(off_t) == sizeof(int)
> > 		printf("%d\n", off_t_var);
> > 	#elif sizeof(off_t) == sizeof(long)
> > 		printf("%ld\n", off_t_var);
> > 	#else
> > 	#error cannot print off_t
> > 	#endif
> >
> But is not on most 32 bit system, both long and int 4 bytes long, so %d
> will be used, but off_t in that case is mostly of type long?

If int and long are both 32 bits, then they're the same type, so it
doesn't matter whether you use "%d" or "%ld".

> > You could also include tests for specific platforms which support "ll"
> > or "q".
> 
> C99 does have a 64 bit integer type, how does one print it here?

C99 defines "%lld" for the *printf family. But C99 support is far from
widespread. [Aside: a significant number of people are still using
Unices which don't provide the wcs* functions, and they are C89.]

-- 
Glynn Clements <glynn.clements@virgin.net>

  reply	other threads:[~2002-06-27 21:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-26 21:30 Howto print off_t Holger Kiehl
2002-06-27  6:09 ` Glynn Clements
2002-06-27 19:24   ` Andrew Edmondson
2002-06-27 20:54     ` Glynn Clements
2002-06-27 19:43   ` Holger Kiehl
2002-06-27 21:18     ` Glynn Clements [this message]
2002-06-28  5:00       ` Holger Kiehl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15643.33068.680130.50185@cerise.nosuchdomain.co.uk \
    --to=glynn.clements@virgin.net \
    --cc=Holger.Kiehl@dwd.de \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).