Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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


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.


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.


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/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: