Live data from Hacker News

So you think you know C? (2016)

wordsandbuttons.online

41–50 of 344 posts

Re: So you think you know C? (2016)

#41
post #40

“I don’t know” is not the right answer. I do know. I know the answer to be “Unspecified”.

But you _don't_ know what it returns, because the behavior is either undefined or implementation defined, and you don't know the implementation.

But you do know that it's undefined or implementation defined. It's a "known unknown".

Re: So you think you know C? (2016)

#42
post #33

There is a minor error in the explanation for the third one: the minimum allowed value for CHAR_BIT in C is 8 (it does not affect the result, principally because the value of ' ' could be anything in the range of char).

That's the one where I wanted most to quibble with the "explanation" of why there is no set answer: To me, the top of the line answer is that the value of the string constant ' ' as an integer is implementation-defined (or maybe it depends on the execution environment? See, I'll get the precise wording wong too); anyway, space is 32 in ASCII but 64 in EBCDIC. The most you could say is that it's not zero (and maybe that it's not -1? I'd have to check how EOF is defined)..

Re: So you think you know C? (2016)

#43
post #30

There are a few problems with the questionnaire. "I don't know" is pretty generic choice to be given/choosen. Say for example, in question 5, the statement "return i++ + ++i;" is undefined, because the value of i is read and modified twice in a single sequence point (and of course, the order of addition is unspecified), which is not allowed in C. So the answer is "Undefined." (The explanation given in the page not ac…

> So the answer is "Undefined."

The code is undefined, but "I don't know." is still the correct answer for what happens to the variable.

Re: So you think you know C? (2016)

#44

“I don’t know” is not the right answer. I do know. I know the answer to be “Unspecified”.

By that logic, someone who says "the origin of handedness is unknown" contradicts himself, since he does know the origin of handedness to be "unknown".

Re: So you think you know C? (2016)

#47
post #30

There are a few problems with the questionnaire. "I don't know" is pretty generic choice to be given/choosen. Say for example, in question 5, the statement "return i++ + ++i;" is undefined, because the value of i is read and modified twice in a single sequence point (and of course, the order of addition is unspecified), which is not allowed in C. So the answer is "Undefined." (The explanation given in the page not ac…

> So the answer is "Undefined." The code is undefined, but "I don't know." is still the correct answer for what happens to the variable.

> The code is undefined, but "I don't know." is still the correct answer for what happens to the variable.

Well, then a better choice would be "I can't know."

Re: So you think you know C? (2016)

#48

This is a perfect example of what I hate about some tests. Didn't quite know the purpose of the test, there's a difference between code for any machine and any compiler and gcc running on some vanilla x86, which is pretty common, and could have been the content (ex: you say it's undefined, that's obvious, everyone knows that already, but it's still deterministic... Here's a breakdown of what happens in practice, blah…

You can easily get bitten by at least one of those examples just by compiling your application written on x86 to ARM to get it running on Android, so not sure if it's as esoteric as you think.

Re: So you think you know C? (2016)

#49
On a typical modern arch, there are consistent and reasonable answers:

1. 8 (https://godbolt.org/z/Udcuj0)

2. 0 (https://godbolt.org/z/kZz1YQ)

3. -96 (https://godbolt.org/z/1Xq_Oo)

4. 1 (https://godbolt.org/z/MIo0s_)

5. 2 (https://godbolt.org/z/J0dsVW)

I agree, you can argue that it's unspecified, undefined, or whatever. It might not be well defined by the C specification, but none of these programs produce surprising output. Programming in the real world requires that you are able to read and write code like this, even if it requires that you investigate (and depend) the specific behaviour of your compiler/platform.

Re: So you think you know C? (2016)

#50

On a typical modern arch, there are consistent and reasonable answers: 1. 8 ( https://godbolt.org/z/Udcuj0 ) 2. 0 ( https://godbolt.org/z/kZz1YQ ) 3. -96 ( https://godbolt.org/z/1Xq_Oo ) 4. 1 ( https://godbolt.org/z/MIo0s_ ) 5. 2 ( https://godbolt.org/z/J0dsVW ) I agree, you can argue that it's unspecified, undefined, or whatever. It might not be well defined by the C specification, but none of these programs produce…

> produce well defined output.

Perhaps “produce consistent results” would be more appropriate.

Post reply on HN