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

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



Mitchell Laks wrote:
On Wednesday 05 April 2006 15:16, Mike McCarty wrote:


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));



Why do I have cast something from size_t? I see the construct I used in "C a reference Manual" by Harbisson and Steele p148.

Well, it is defective. The C Standard simply states that the
type returned by the sizeof operator is an unsigned integer
type, but which one is implementation dependent. Since you
don't know whether it be an unsigned short int, or an
unsigned long int, you have to cast, because they have
(potentially) different formats, and definitely different
format specifiers. In any case, the use of "%d" which
specifies a signed integer conversion is just wrong,
since sizeof returns an unsigned integer type.


I am not a c expert - and I dont understand why you do the cast.

Everybody is ignorant, just about different things.[*]
Well, I just gave you the explanation.

Do you have an example of a compiler where the length of sizeof(int) is greater than size_t ?

Umm, it's likely to be the other way around. In any case, using
a signed integer conversion format specifier with an unsigned
integer type is wrong.

[snip]

* Will Rogers

Mike
--
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
You have found the bank of Larn.
I can explain it for you, but I can't understand it for you.
I speak only for myself, and I am unanimous in that!



Reply to: