[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: amd64: why is sizeof(int) =4? why not =8?



2006-04-05 21:16 +0200, Mike McCarty <Mike.McCarty@sbcglobal.net>:
> Mitchell Laks wrote:
> > [...]
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> >
> > typedef struct employee_st {
> >     char name[40];
> >     int id;
> > } Employee;
> >
> > int main()
> > {
> >     int myInt;
> >     Employee john;
> >
> >     printf("Size of int is %d\n",sizeof(myInt));
>
> This program has defects. The type of the results of
> the sizeof operator is an unsigned integer of unspecified
> size. The only portable way to do this is...
>
>      printf("Size of int is %lu\n",(unsigned long)sizeof(int));

The result of sizeof is a size_t, an unsigned type. Couldn't it happen
to be using something bigger than a unsigned long, such as an unsigned
long long? If I have read it correctly, using a C99-compatible
compiler (ISO/IEC 9899:1999) one could simply write

    printf("Size of int is %zu\n", sizeof(int));

where z is a length modifier (akin to l o ll) that that specifies a
size_t when printing integers (i.e. with d, u, o, x et al.).

--
Gonzalo HIGUERA DÍAZ <gonhidi@gmail.com>



Reply to: