C Programming Puzzlers
stevenkobes.com
C Programming Puzzlers
1–10 of 32 posts
Re: C Programming Puzzlers
#2Aside: 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
#3Re: C Programming Puzzlers
#4Puzzles 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…
"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
#5Puzzles 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…
Yes, I meant question 14, not 12.
Re: C Programming Puzzlers
#6These are pretty much the reason that Go exists.
Re: C Programming Puzzlers
#7---
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
#8Puzzles 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…
Edit: clarified that I am referring to local arrays only
Re: C Programming Puzzlers
#9 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
#10Edit : 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…
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.