Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Puzzles 9 and 12 assume sizeof(int) is 2. It's poor form for a language lawyering quiz to make an assumption about implementation-dependent behavior. It's especially poor form when the assumption made is long outdated and likely to surprise--I suppose it's still true for C compilers targeting 8-bit and 16-bit microcontrollers.

Aside: I remember buying the first Programmer's Heaven CD-ROM boxed set at The Party back in the mid-nineties. It was an incredibly valuable resource for me in those days. The best source of programming information was through the dial-up BBS scene, but none of the boards I frequented had a collection on the scale of Programmer's Heaven.



From question 9:

"What is the output of this program on an implementation where int occupies 2 bytes?"

Question 12 is a function pointer question and doesn't say anything about ints at all.

I'll assume you meant Question 14:

"What is the output of this program on an implementation where int and all pointer types occupy 2 bytes?"

If you read the question you would have noticed that the assumption was put in writing so that even if you first assumed int was 4 bytes, or pointers were 4 bytes you would still be able to find the answer correctly.


Ah, I had clicked through to the original article which does not contain those caveats.

Yes, I meant question 14, not 12.


Was it possible to initialise a local array with literals like that (e.g. Q5) in C89? If not then the version of C in the examples (assuming it is consistent) must be at least C99, which suggests that sizeof(int)==2 is due to the target being an embedded device rather than the program being old.

Edit: clarified that I am referring to local arrays only


It was not only possible to initialize arrays like that in C89, that method is often referred to as "C89-style".

Also, let's not forget what the C standard says about integer sizes, which is that

A) It is guaranteed that sizeof char == 1

B) It is guaranteed that the following holds true: sizeof char <= sizeof short <= sizeof int <= sizeof long

So it's always completely unrealistic to assume anything about the size of these types on any system, except for the guarantee in (A)


A char might always be one byte but one byte isn't necessarily 8 bits.

I'm often puzzled by the fact that the size of basic datatypes isn't platform independent.

If I need to count to x I, more often than not, need to count to x on all platforms. Yet for some reason that is something that varies on platform.

Yes, there are of course times when this is useful but that should be the exception (right?). And apparently the industry is on my side on this by making the situation even worse and saying that an integer on a 64 bit x86-machine should be 4 bytes. Now the sizes of the datatypes not only vary by platform but they also have nothing to do with the underlying hardware architecture either. It's just some arbitrary number that is different on platforms just for the sake of giving you a headache. Thanks?

Of course a large datatype, x, might incur a severe performance penalty on platform y but just changing the size of x behind my back is not constructive.


I think the idea originally was that int would usually be the preferred "natural" size for arithmetic on any given platform. Longer sizes are available when you need greater range, and smaller sizes are available when you're trying to conserve memory, but both may be slower than int (because of the possible need for multiple-word arithmetic in the longer sizes, and the possible need for sub-word operations in the smaller sizes).

So int is for things like loop indices, where efficiency of arithmetic is more important than size in memory.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: