public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* %d or %ld for datatype int?
@ 2003-12-22 20:23 Rich Johnson
  2003-12-22 20:50 ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Rich Johnson @ 2003-12-22 20:23 UTC (permalink / raw)
  To: gcc-help

Hi folks--

I'm having trouble getting the following to compile cleanly on multiple 
platforms.  Both platforms are using gcc 3.3:

#include <sys/time.h>
#include <stdio.h>
int main(){
struct timezone tz;
struct timeval now;
gettimeofday (&now, &tz);
printf (  "%ld seconds into the epoch\n", now.tv_sec );
return 0;
}

"gcc -Wall ..."  on OSX (10.2.8) reports:

test.c: In function `main':
test.c:8: warning: long int format, int32_t arg (arg 2)

where tv_sec is an "int32_t" which is an "int" which is a 32-bit integer

If I change the "%ld" to "%d", then compilation on a linux platform 
reports:

test.c: In function `main':
test.c:8: warning: long int format, __time_t arg (arg 2)

tv_sec is a "__time_t" which is a "long int" which is a 32-bit integer.

What gives here?  Do I have to explicitly cast the tv_sec to a "long 
int" to get a clean compilation on both platforms?
It just seems odd that in both cases the warning boils down to
"warning: 32bit integer used where 32bit integer expected"

Thanks,
--rich

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: %d or %ld for datatype int?
  2003-12-22 20:23 %d or %ld for datatype int? Rich Johnson
@ 2003-12-22 20:50 ` Eljay Love-Jensen
  2003-12-22 22:13   ` Rich Johnson
  0 siblings, 1 reply; 3+ messages in thread
From: Eljay Love-Jensen @ 2003-12-22 20:50 UTC (permalink / raw)
  To: Rich Johnson, gcc-help

Hi Rich,

Try this:
printf (  "%ld seconds into the epoch\n", (long int)now.tv_sec );

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: %d or %ld for datatype int?
  2003-12-22 20:50 ` Eljay Love-Jensen
@ 2003-12-22 22:13   ` Rich Johnson
  0 siblings, 0 replies; 3+ messages in thread
From: Rich Johnson @ 2003-12-22 22:13 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help


On Monday, December 22, 2003, at 03:51 PM, Eljay Love-Jensen wrote:

> Hi Rich,
>
> Try this:
> printf (  "%ld seconds into the epoch\n", (long int)now.tv_sec );
>
Thanks,  I don't know why but it takes me a bit of effort to accept the 
need for a cast--especially after all the effort that went into 
typedefs for int32_t, __time_t, etc.

I  rationalize the cast as producing "what printf" expects rather than 
what "the data is".  It also helps to realize that it will work with 
64-bit architectures, no matter what __time_t is defined to be.

--rich

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-12-22 22:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-22 20:23 %d or %ld for datatype int? Rich Johnson
2003-12-22 20:50 ` Eljay Love-Jensen
2003-12-22 22:13   ` Rich Johnson

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 read-only IMAP folder(s) and NNTP newsgroup(s).