* Howto print off_t
@ 2002-06-26 21:30 Holger Kiehl
2002-06-27 6:09 ` Glynn Clements
0 siblings, 1 reply; 7+ messages in thread
From: Holger Kiehl @ 2002-06-26 21:30 UTC (permalink / raw)
To: linux-c-programming
Hello
What is the best way to print an off_t variable. On a 32 bit machine
you can print it with %ld and on a 64 bit machine you need to print
it with %lld. One could code this as follows:
if (sizeof(off_t) == 4)
printf("%ld\n", off_t_var);
else
printf("%lld\n", off_t_var);
But is this portable? Do all implementations know about %lld? Don't
some of them use %Ld or some other syntax.
Or is there a better way of doing this? I don't like the above code
since it makes it ugly to read when you have to print lots of off_t's.
Thanks,
Holger
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Howto print off_t
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 19:43 ` Holger Kiehl
0 siblings, 2 replies; 7+ messages in thread
From: Glynn Clements @ 2002-06-27 6:09 UTC (permalink / raw)
To: Holger Kiehl; +Cc: linux-c-programming
Holger Kiehl wrote:
> What is the best way to print an off_t variable. On a 32 bit machine
> you can print it with %ld and on a 64 bit machine you need to print
> it with %lld.
On a 64-bit machine, "long" is probably 64 bits.
> 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
> 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
You could also include tests for specific platforms which support "ll"
or "q".
> Do all implementations know about %lld?
No.
> Don't some of them use %Ld or some other syntax.
"%Ld" is invalid; the "L" modifier is only valid when preceding a, A,
e, E, f, g, or G, and indicates a "long double" argument.
> Or is there a better way of doing this? I don't like the above code
> since it makes it ugly to read when you have to print lots of off_t's.
Assuming that off_t is no larger than "long", you could just cast it,
e.g.:
printf("%ld", (long) off_t_var);
If off_t is larger than "long", then you have to deal with platform
specifics, as ANSI C doesn't have anything bigger than long.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Howto print off_t
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
1 sibling, 1 reply; 7+ messages in thread
From: Andrew Edmondson @ 2002-06-27 19:24 UTC (permalink / raw)
To: linux-c-programming; +Cc: Holger.Kiehl
On Thu, 27 Jun 2002, Glynn Clements wrote:
> Holger Kiehl wrote:
> > What is the best way to print an off_t variable. On a 32 bit machine
> > you can print it with %ld and on a 64 bit machine you need to print
> > it with %lld.
>
> On a 64-bit machine, "long" is probably 64 bits.
>
> > Do all implementations know about %lld?
>
> No.
Technically no, but c99 defines long long with the modifier ll in conjunction
with d, i, o, u, x, X so %lld is valid and should be portable.
--
Andrew Edmondson
Test Development Engineer
<-------------------------->
The discerning heart seeks knowledge,
but the mouth of a fool feeds on folly.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Howto print off_t
2002-06-27 6:09 ` Glynn Clements
2002-06-27 19:24 ` Andrew Edmondson
@ 2002-06-27 19:43 ` Holger Kiehl
2002-06-27 21:18 ` Glynn Clements
1 sibling, 1 reply; 7+ messages in thread
From: Holger Kiehl @ 2002-06-27 19:43 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
On Thu, 27 Jun 2002, Glynn Clements wrote:
>
> Holger Kiehl wrote:
>
> > What is the best way to print an off_t variable. On a 32 bit machine
> > you can print it with %ld and on a 64 bit machine you need to print
> > it with %lld.
>
> On a 64-bit machine, "long" is probably 64 bits.
>
> > 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.
> > 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?
> 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?
Thanks,
Holger
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Howto print off_t
2002-06-27 19:24 ` Andrew Edmondson
@ 2002-06-27 20:54 ` Glynn Clements
0 siblings, 0 replies; 7+ messages in thread
From: Glynn Clements @ 2002-06-27 20:54 UTC (permalink / raw)
To: linux-c-programming
Andrew Edmondson wrote:
> > > What is the best way to print an off_t variable. On a 32 bit machine
> > > you can print it with %ld and on a 64 bit machine you need to print
> > > it with %lld.
> >
> > On a 64-bit machine, "long" is probably 64 bits.
> >
> > > Do all implementations know about %lld?
> >
> > No.
>
>
> Technically no, but c99 defines long long with the modifier ll in conjunction
> with d, i, o, u, x, X so %lld is valid and should be portable.
Well, portable to all C99-conforming implementations, which is
basically the same thing as "not portable".
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Howto print off_t
2002-06-27 19:43 ` Holger Kiehl
@ 2002-06-27 21:18 ` Glynn Clements
2002-06-28 5:00 ` Holger Kiehl
0 siblings, 1 reply; 7+ messages in thread
From: Glynn Clements @ 2002-06-27 21:18 UTC (permalink / raw)
To: Holger Kiehl; +Cc: linux-c-programming
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>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Howto print off_t
2002-06-27 21:18 ` Glynn Clements
@ 2002-06-28 5:00 ` Holger Kiehl
0 siblings, 0 replies; 7+ messages in thread
From: Holger Kiehl @ 2002-06-28 5:00 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming
On Thu, 27 Jun 2002, Glynn Clements wrote:
>
> 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
> ...
>
I already had plans to use autoconf to do the configuration, so this
will be one more reason for using it.
Many thanks for the help!
Holger
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2002-06-28 5:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2002-06-28 5:00 ` Holger Kiehl
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).