Live data from Hacker News

So you think you know C? (2016)

wordsandbuttons.online

251–260 of 344 posts

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

#252
But a code based on several standards, not only on C standard. For example we know, that basic source character sets should contain space (C11 5.2.1), we know that character constants has type "int" and that character constants represent value equal to the code of a symbol (C11 5.4.4). We know used source character set and we know code of the space character. We can configure specific source character set on the POSIX compatible systems. We know that return statement in the main function equivalent to the call to exit function (C11 5.1.2.2.3), we know that only 8 least significant bits will be used from returned value (POSIX.1-2001 definition of "exit"), we know that INT_MAX should be at least 32767 (C11 5.2.4.2.1) so we sure that the result that we got from "return" statement in the "main" function is positive integer from 0 to 512. Finally if we configure source character set to be sure that ' ' has code 32 we know for sure that we got value 416 in the specified example. So we know for sure answer on question 3 based on the C11, POSIX.1-2017, and ISO 646 standards.

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

#253

Earlier quoted context omitted.

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.

Or on different compilers, or the same compiler with different options, etc.

Or a new release of the same compiler.

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

#254

Earlier quoted context omitted.

> You can get along just fine in C++ without knowing the exact aliasing rules from C Nope, you can't. These are exactly the things that introduce undefined behavior (i.e. total breakage) if you aren't very careful about what you're doing at all times. Don't take my word for it, check out what the C++ designers themselves state about the issue in the C++ Core Guidelines. C/C++ is far from simple, and thinking that you…

You are absolutely right, but my point is that when you do modern C++, in the application code, it is very unlikely that you need to use reinterpret_cast in your code, and therefore you don't need to know all the subtlety about it. So despite C++ being more complex than C, if you limit yourself to some practical subset, it is actually easier than C.

Same can be (and has been) said about JavaScript. Or C. Or any language with any kind of issues.

And it doesn't work in practice.

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

#255

Earlier quoted context omitted.

But the point is that I do know what that will print on my computer(s), with my compiler(s), on my architecture(s). "I don't know" is too generic a statement.

Even on a given CPU with a given compiler you usually do not know until you've tried. This is an article about the C language and the starting point with all the examples is that you do not know for a fact what the result will be.

I understand your claim. But I've gotta say this claim is maybe a little too aggressive. I know for a fact that on a Sun-3 (68020 SunOS Desktop Pizza Box) using either gcc or the bundled cc, all of them would have been the same answer, and the answer would have been known to the coder, before running that code (unless you unleashed one of the gcc command line dogs). Except maybe #5, because who does this?

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

#256
post #250

Earlier quoted context omitted.

To me "I don't know" is a very apt choice. It makes the point clear that indeed reading the code does not allow you to know the result, which is quite a pitfall. In your comment you are jumping from "I don't know", which is the first step, to wanting to explain why.

There were multiple questions where I would have answered "It's undefined" or "It's implementation defined", but those weren't options. It's not that I don't know the answer; I know the answer ("it's implementation defined according to the spec, but on essentially every relevant platform, the result will be X"), but the "it's implementation defined" part of my answer isn't an option, so the only possible answer becom…

the quiz asks what each one would evaluate to. “Undefined” or “implementation dependent” are not answers to that question. “I don’t know [because it is undefined]” or “I don’t know [based on the given information]” are logically consistent answers to the question that was asked

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

#257
post #252

But a code based on several standards, not only on C standard. For example we know, that basic source character sets should contain space (C11 5.2.1), we know that character constants has type "int" and that character constants represent value equal to the code of a symbol (C11 5.4.4). We know used source character set and we know code of the space character. We can configure specific source character set on the POSI…

I don't know if POSIX specifies CHAR_MAX, but on most systems, storing the value into a char will change the value, making it everything but 416: https://tio.run/##S0oszvifnFiiYFeSWlyil6xgY@Pq7/Y/M69EITcxM0...

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

#258
The author's explanations of the first three answers aren't sufficient. There is no requirement within C for `int` and `char` to be different sizes. Similarly, you don't know what the resolution of ' '*13 will be. It's architecture dependent.

C, for all it's simplicity, is a relatively complex language.

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

#259
I scored 2, but mostly out of luck for being 100% sure only about the 1st question, as I encountered alignment problems a lot of times in the past when dealing with structures to be sent through the network, and also between different architectures (and sometimes endianess too). If memory serves, there are #pragma directives to force the compiler to align structure members to a given interval, but they're compiler dependent and would make a non portable piece of code even less portable.

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

#260
post #252

But a code based on several standards, not only on C standard. For example we know, that basic source character sets should contain space (C11 5.2.1), we know that character constants has type "int" and that character constants represent value equal to the code of a symbol (C11 5.4.4). We know used source character set and we know code of the space character. We can configure specific source character set on the POSI…

I don't know if POSIX specifies CHAR_MAX, but on most systems, storing the value into a char will change the value, making it everything but 416: https://tio.run/##S0oszvifnFiiYFeSWlyil6xgY@Pq7/Y/M69EITcxM0...

[deleted]
Post reply on HN