I got 4/5:)
Am I the only one who enjoyed working out what (I thought were) the answers in my head, and was then delighted to see I was wrong? Devious :)
The questions assume a particular set of implementation characteristics, so the post ought to have the title “So you think you know how a particular C implementation works?”
More than you want to know about C.
More specifically, it does not offer “Implementation-defined behavior” or “undefined behavior”—the correct answers—as options.
The answers to these questions are not unknowable, the semantics are just not defined by ISO C. But knowing ISO C in a vacuum isn’t helpful; in order to actually write C, you need to be familiar with an implementation (your compiler).
I get that it’s popular to rag on C these days (a lot of the criticisms are well-founded after all), but quizzes like this offer no benefit to the discussion.
Agreed, this is a terrible “quiz”. C has a helluva lot of real pitfalls… but most of these aren’t real pitfalls. “What would the Deathstation 9000 do” isn’t really a useful question. Undefined behavior is one thing, but “ah, but you see, what if you were running this code on a PDP-11? what then?” is truly silly nitpickery.
Much of programming C -is- knowing what the typical implementation behavior is, for better or worse.
A big problem is that typical implementation behavior is changing as the developers of GCC and Clang seem to be hell bent on taking any undefined behavior and using it to optimize down to those those last 3 or 4 instructions, regardless of how many nasal demons will be released. New optimizations breaking existing code makes it important to avoid undefined behavior.
I had really high hopes for John Regehr’s ‘Friendly C’ dialect, but it seems dead in the water: http://blog.regehr.org/archives/1180
I think that that’s the point. :)
(Spoiler Alert.) The problem is that fleshing out “I don’t know” (the correct answer) into “implementation defined” as @halosghost suggests would give away the “twist”. Many of these constructions look like valid (i.e. no undefined/implementation-defined) code to intermediate-level C programmers like me, but are not.
No, I think I know what sane code does. I’ve been lucky enough to work at companies where programmers mostly don’t write poor code.
I generally dislike this sorts of implementation quizzes as I don’t feel they generally help the discussion, however I like how the author ends with
And at this point I only have to apologize. The test is clearly provocative and may even be a little offensive. I’m sorry, if it causes any aggravation.
I only hope this little test would help someone like myself from the past to learn this attitude in some 15 minutes and not 15 years.
Which I actually consider healthy and useful.
C is a simple language, in so far as the specification is small - but this simplicity has holes and this in turn leads to complexity in implementations.
My normal answers to quizzes like this is: irrelevant, I wouldn’t accept that code in a project I worked on, and it would be easy to decipher if I had a working implementation to play with.
In this case though, I think the article is actually interesting in that the author is trying to convey the point of “I don’t know” (undefined / implementation defined).
If you’d like to quiz yourself on Java, these are really tricky:
Except all these ones come from the specification, nothing implementation specific!
I got 2/5 (and that’s only becuase I have worked a little with embedded platforms and am aware of pitfalls with non-standard C compilers). I would have never guessed that the last 2 weren’t specified in the standard. I am now going to find it and read it.
Nice time to question the fundamentals, considering the US presidential election results. Sorry, everything seems related to this today.