Live data from Hacker News

C Programming Puzzlers

stevenkobes.com

1–10 of 32 posts

Re: C Programming Puzzlers

#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 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.

Re: C Programming Puzzlers

#4
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…

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.

Re: C Programming Puzzlers

#5
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…

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…

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

Yes, I meant question 14, not 12.

Re: C Programming Puzzlers

#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.

Re: C Programming Puzzlers

#7
Edit: I was wrong - see cygx's reply

---

I'm a bit rusty on the C spec, but I think the author is wrong about #4.

The answer refers to an exception for a pointer that points one past the end of an array.

And &a[5] would be valid based on that rule, because it points one past the end of the array a.

However (&a + 1) is not valid. It does not point one past the end of an array (because although a is an array, it is not an element of an array.)

Having said that I would be surprised if any compiler gave a result other than "2 5". Still, it's technically undefined.

Re: C Programming Puzzlers

#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

Re: C Programming Puzzlers

#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.

Re: C Programming Puzzlers

#10
post #7

Edit : I was wrong - see cygx's reply --- I'm a bit rusty on the C spec, but I think the author is wrong about #4. The answer refers to an exception for a pointer that points one past the end of an array. And &a[5] would be valid based on that rule, because it points one past the end of the array a. However (&a + 1) is not valid. It does not point one past the end of an array (because although a is an array, it is no…

See C99 6.5.6 §7:

For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

Post reply on HN