News for hackers. Stuff that matters.
Re:So you think you know C? by awalton on Tuesday November 08, 2016 @03:54PM (#12903844)
If you write a quiz like this, and "Don't Know" is an answer but "Undefined Behavior" and "Implementation-Dependent" are left off, you're doing it wrong.
Re:So you think you know C? by kmill on Wednesday November 09, 2016 @12:28AM (#12906341)
The question is "what would the return value be," and without knowing which compiler is used you don't know what it would return. It doesn't make sense to say the program returns "undefined behavior" or "implementation-dependent."

You might be taking issue with the question itself, but I think it's a valid question because C compilers tend to be lenient with what they compile.

Re:So you think you know C? by gkya on Wednesday November 09, 2016 @02:35AM (#12907188)
I can't know != I don't know
Re:So you think you know C? by amelius on Wednesday November 09, 2016 @05:56AM (#12909149)
Yes, and "I can't know" is a stronger assertion than "I don't know". The former implies the latter. However, both are correct.
Re:So you think you know C? by gkya on Wednesday November 09, 2016 @08:59AM (#12910259)
Thing is I can know if the necessary information is provided (i.e. CPU architecture, version of the standard). Portability is a feature of C, but C's portability is mostly syntactic and only partly semantic.
Re:So you think you know C? by jwdunne on Thursday November 10, 2016 @02:06AM (#12918342)
But if the necessary info isn't provided, you don't know because, as you say, you can't know.

It's semantics. On many exams, "don't know" is a correct answer when you can't know because info is intentionally left out.

Re:So you think you know C? by gkya on Thursday November 10, 2016 @10:42AM (#12921303)
I don't really want to be a pedant here, but in this case if the necessary information was provided, I had the knowledge to find a correct answer. This is different from the case where even if the information was provided I wouldn't be able to know because I don't have the necessary knowledge.
Re:So you think you know C? by cpkpad on Tuesday November 08, 2016 @09:46PM (#12905736)
Indeed. The quiz doesn't start with "What does the ANSI C standard guarantee about..." It says "So you think you know C." C is a cross-platform assembly language. I use it when I need to code to the metal. I know exactly how it will behave in most of those scenarios.

I understand, in abstract, I might be on an EBCDIC system, and things will be different. But that's not the reality of C. C isn't just a formal specification.

I spent a while on 1, thinking about how my compiler would handle that. 2 I was 90% confident about. 3 and 4 I was 100% confident about.

Re:So you think you know C? by rkangel on Wednesday November 09, 2016 @08:35AM (#12910097)
> C isn't just a formal specification

No, C IS just a formal specification.

What you are talking about is C on your particular compiler on your particular platform. If you are writing C code just to be used for that scenario then you can use all of the implementation understanding you want.

If, however, you want to write C that will have consistent behaviour across multiple compilers and multiple platforms, then you need to limit yourself to the behaviours that the standard guarantees. Otherwise the compiler behaviour may change and your program behaviour will change.

Even between different versions of the same compiler implementation defined behaviour can change (although assumptions about sizes probably won't).

Re:So you think you know C? by yoklov on Wednesday November 09, 2016 @09:04AM (#12910293)
A lot of the implementation defined behavior can be ensured with static assertions (either via new-fangled C11 _Static_assert or via the old fashioned negative length array hack), particularly WRT sizes.
Re:So you think you know C? by rkangel on Wednesday November 09, 2016 @09:26AM (#12910477)
Absolutely, and that's a big step forward to writing code that will silently do bad things on a different platform. I use that technique on my current project to assert a load of things that I know are true.

It doesn't get you as far as the ideal goal which is to write code that will silently work on another platform. Static asserts mean that you then have to go and write more code when they happen.

Re:So you think you know C? by jhomedall on Thursday November 10, 2016 @09:47AM (#12920762)
I know exactly how it will behave in most of those scenarios.

Right up until an optimizing compiler throws your assumptions under the bus.

Compilers are allowed to (and both GCC and Clang frequently do) assume that your program will never invoke undefined behavior, and optimize accordingly.

See: http://blog.llvm.org/2011/05/what-every-c-programmer-should-... Also maybe: http://blog.regehr.org/archives/1307

This can lead to some pretty bad security flaws: https://lwn.net/Articles/575563/

Re:So you think you know C? by simosx on Tuesday November 08, 2016 @04:00PM (#12903890)
People would figure it out from the start if "Implementation-Dependent" was an option. Perhaps a "Something else" would be more appropriate so that the reader would still be surprised at the end and feel it was a decent multiple-choice test.
Re:So you think you know C? by cortesoft on Tuesday November 08, 2016 @07:56PM (#12905309)
The purpose of this quiz is not to gauge the taker's knowledge of the subject, it is to make a point.
Re:So you think you know C? by anindha on Tuesday November 08, 2016 @05:59PM (#12904710)
I wasn't confused. The first one is pretty obvious it's unknown and "I don't know" is the closest answer.
Re:So you think you know C? by tedunangst on Tuesday November 08, 2016 @04:59PM (#12904324)
Indeed. It's not like the answers are unknowable.
Re:So you think you know C? by shp0ngle on Tuesday November 08, 2016 @06:36PM (#12904966)
It's pretty clear it's tongue-in-cheek.
Re:So you think you know C? by sqeaky on Tuesday November 08, 2016 @04:11PM (#12903974)
It is likely that the state of someone's knowledge is "don't know" regardless of the actual state of affairs.

If a person wanting to improve guesses correctly, did they actually learn anything? Can they use that next time? They definitely can if they get it wrong and have a wrong answer to start research with.

Edit - I see that I misread your comment, please disregard this. I thought the quiz had don't know and the other options you specified.

Re:So you think you know C? by acveilleux on Tuesday November 08, 2016 @04:19PM (#12904034)
Or perhaps you're not a native english speaker?