Live data from Hacker News

C Programming Puzzlers

stevenkobes.com

11–20 of 32 posts

Re: C Programming Puzzlers

#11
post #8
post #2

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 i…

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 So it's always completely unrealistic to assume anything about the size of these types on any system, except for the guarantee in (A)

Re: C Programming Puzzlers

#12
post #9

For question 7, can someone confirm that int a[][3] = {1, 2, 3, 4, 5, 6}; is not a typo and is, in fact, a valid array initialization? I've never come across this and think it's rather unintuitive.

Checking the rules for array initialization in Harbison & Steele, this does indeed seem to be invalid. I'm surprised, I actually thought that was valid...

Re: C Programming Puzzlers

#13
post #9

For question 7, can someone confirm that int a[][3] = {1, 2, 3, 4, 5, 6}; is not a typo and is, in fact, a valid array initialization? I've never come across this and think it's rather unintuitive.

It's valid (see C99 6.7.8 §20 and §22) and equivalent to

    int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };

Re: C Programming Puzzlers

#16
post #6
post #3

These are pretty much the reason that Go exists.

These are not the problem with C. Things like a lack of support for functional programming constructs and more powerful manipulation of structs (e.g. iterating over fields and inspecting variable types) would be much more improvement to C than minor cosmetic changes that would hide the details that this quiz asks about.

Lol.

Re: C Programming Puzzlers

#17

Stopped at #3. It seems incorrect. The answer states that foo returns x to the power of n. But I ruled that answer out since any 0 or negative n is going to return 1. edit: added emphasis

Returning 1 for n = 0 is OK, as x^0 = 1 for all non-zero x, and 0^0 is usually also defined as 1.

However, as you note, it also returns 1 for negative n, and that is just wrong.

Well, wait a second....actually, if it were a machine where integer division rounds up[1], so that 1/k = 1 for k > 1, then #3 would correctly compute x^n for n Nope!

Their code for n > 0 would fail on such a machine. They are relying on repeated division by 2 eventually resulting in 0 in order to terminate the recursion. On a round up machine, repeated division converges to 1, not 0.

[1] I'm assuming that rounding behavior of integer division is implementation defined in C as defined at the time that quiz was written.

Re: C Programming Puzzlers

#18
post #11
post #8

Earlier quoted context omitted.

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 So it's always completely unrealistic to assume anything about the size of these types on any system, except for the gu…

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.

Re: C Programming Puzzlers

#20
post #6
post #3

These are pretty much the reason that Go exists.

These are not the problem with C. Things like a lack of support for functional programming constructs and more powerful manipulation of structs (e.g. iterating over fields and inspecting variable types) would be much more improvement to C than minor cosmetic changes that would hide the details that this quiz asks about.

You've obviously know very little about the motivation of Go then...
Post reply on HN