Live data from Hacker News

So you think you know C? (2016)

wordsandbuttons.online

91–100 of 344 posts

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

#91
post #41
post #40

Earlier quoted context omitted.

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

Look, do you know what will it return or not?

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

#92

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…

Consistent as in works for me and I ran it twice!, not consistent as in guaranteed to continue working after sudo apt upgrade gcc. Undefined behaviour can and will be used to make assumptions for optimisations that will bite you.

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

#93

Earlier quoted context omitted.

Switch the compiler to ARM, which is as "typical modern arch" as it gets, and see for yourself.

I don't think it's surprising when you switch machine architecture and/or word sizes that you get different results. In fact for me, that's completely normal and to be expected.

I sure hope you're not writing the libraries that I use in C/C++ :)

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

#94

Isn't there a more fundamental flaw in these questions? main() always returns an int, whether that's 4 or 8 bytes, 0 or 1 means success or failure depends on the implementation. here's a bit of a discussion https://stackoverflow.com/questions/204476/what-should-main-... Reminds me of the dumb exams some teachers would set to trick you when in school to make themselves feel superior.

What's wrong with it returning int here? They aren't using any variables bigger int, and the meaning of return codes is irrelevant.

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

#96
post #2

tl;dr - C has some undefined behavior, if you plan on things working based on your experience with one compiler and one computer, you will be surprised.

I wonder how many C programmers have experience with writing multi platform/compiler code. I have done a lot of C and C++ but never had to port it so I would not be surprised if I had made a ton of mistakes.

[deleted]

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

#97
post #75

Earlier quoted context omitted.

Where the author goes wrong is in assuming that somehow "I don't know" can be a final answer to these things. No, it is absolutely fucking vital that you know how the compiler will pad your structures in C. Similarly to the "what size is an int" on your architecture - on an ATmega8 this is 16 bit, but the chip can't actually do all 16 bit operations in single instructions.

I took that to be the point of the article though, that just looking at the code wasn't enough to know and you needed to go further to answer these cases for your exact use case or target platform.

Further: Unless your code is compiled, deployed to a rocket, and fired off the Earth never to return, the question of “what is my platform?” is meaningless in the context of writing good C.

So, today, using the compiler installed on your system right now, sizeof(int) = 32. Great. That means nothing, and changes nothing about whether your code is correct. You should not write code relying on it. Just like you should not measure the output of the questions on this test, and declare that you know what the answers are.

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

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

Somewhat related, my introductory classes involved a lot of games around pre- and post- increments and short circuiting. While I get that understanding these operations is fundamentally important, is understanding ridiculous combinations of them important? I mean, these were the basis of large portions of some quizzes and midterms. I get playing with them from a theoretical perspective, as this can literally be done…

I don't know how convoluted the questions on your midterms were but one good reason that kind of irritating thing pops up in tests is that it's quite common in real world C code. Think of the old K&R string copy example.

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

#99

Isn't there a more fundamental flaw in these questions? main() always returns an int, whether that's 4 or 8 bytes, 0 or 1 means success or failure depends on the implementation. here's a bit of a discussion https://stackoverflow.com/questions/204476/what-should-main-... Reminds me of the dumb exams some teachers would set to trick you when in school to make themselves feel superior.

Omitting the return type for main is legal in C89.
Post reply on HN