%d or %ld for datatype int?

Rich Johnson rich@shawmutav.com
Mon Dec 22 20:23:00 GMT 2003


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



More information about the Gcc-help mailing list