Live data from Hacker News

The Descent to C (2013)

chiark.greenend.org.uk

61–70 of 96 posts

Re: The Descent to C (2013)

#61
post #56
post #50

Earlier quoted context omitted.

Yeah you have to do something like this if you want to truly raise eyebrows. /\ */ best c comment *\ /

One trick that I like is replacing { and } with .

Wow, a new C trick I didn't know about. What's the history here?

Also, gross.

Re: The Descent to C (2013)

#63

Surprise: there's a whole bunch of different reality under your the convenient illusions of your "high level" programming language. Hardware is the reality. It's not very much like the Java or Python programming model. We shouldn't hide this from programmers.

I disagree. C is also an abstraction of reality over assembly, assembly is an abstraction over machine code, which abstracts microcode, which abstracts over logic gates, and there are both deeper and adjacent abstractions as well. Underlying it all are different mathematical abstractions from karnaugh maps to quantum physics.

A good developer will IMO select an abstraction that best matches their goals. If you’re writing a device driver then the abstraction might be assembly language. If you’re writing business logic it might be PL/pgSQL. But regardless of which abstraction you choose, you’re hiding something from someone.

Re: The Descent to C (2013)

#64
post #55

> (In fact, that's what array[i] means – the language defines it to be a synonym for *(array+i).) To really drive home the primitiveness of C arrays, should probably also mention that, because addition is commutative, you could also write i[array] and somewhat surprisingly, it will compile, and work, and it means "*(i + array)" which is equivalent to "*(array + i)" But nobody really does that, because that would be k…

This is actually a really good way to drive home that arrays don't really "exists" in C and are just syntaxic sugar for pointer arithmetic.

"Because real programmers only need void*".

Re: The Descent to C (2013)

#65
post #61
post #56

Earlier quoted context omitted.

One trick that I like is replacing { and } with .

Wow, a new C trick I didn't know about. What's the history here? Also, gross.

On some ancient systems { and } don't exist. So an alternarive made of more common characters is provided. There is also a two-character combination for [ and ] and a three character combination for |, & and some other characters. The feature is called digraphs/trigraphs and is disabled on most modern compilers by default.

Re: The Descent to C (2013)

#66

"The Python interpreter is written in C, for example." https://github.com/RustPython/RustPython

THE Python interpreter is written in C. People usually refer to CPython when phrasing it like. That does not mean there are no python implementations in other languages, but they all have minuscule market share compared to CPython.

Re: The Descent to C (2013)

#67
post #51

It's not inherent in C being a low-level language that it has such a painful memory model. It's a consequence of having to fit the compiler into a really tiny machine by modern standards. Originally, there were no function prototypes. I once proposed a backwards-compatible way out for C.[1] Basically, you get to talk about arrays as objects with a length, even if that length is in some other variable. And you get sli…

It's similar to what I proposed in 2009:

https://www.digitalmars.com/articles/C-biggest-mistake.html

except mine is much simpler :-)

The proven utility and effectiveness of this is apparent in D.

Re: The Descent to C (2013)

#68
post #43

The following is NOT a criticism of C. Just pointing out different problem domains. > Modern high-level languages generally try to arrange that you don't need to think > or even know – about how the memory in a computer is actually organised Modern high-level languages try to arrange that you don't need to focus on the irrelevant. If you're working on, say, an accounting system, memory layout is not part of the probl…

> For low level operations you probably cannot beat C.

It's not hard to beat it. For example, you cannot do vector operations in C. (Hence C compilers often offer extensions.)

Re: The Descent to C (2013)

#69
post #49

> So why is C like this, anyway? Worth mentioning that C is over 40 years old, and was designed to be easily portable across a range of machines that had less compute power and memory than today's smaller microcontrollers. As a result, a lot of things were left undefined, or were designed in a way to be easy to implement rather than easy to program for . There existed other programming languages that were better, but…

> left undefined, or were designed in a way to be easy to implement rather than easy to program for. I'd tweak your statement a little, or even a lot: "left undefined" most often meant "left to be defined by the compiler writers to fit the architecture of the underlying hardware, in a way that would make it easy to program to beneficially exploit features of the architecture"; and (yes) in a way "that would not be ve…

Sorry, you're absolutely right, and I was lazy in my comment.

There is indeed a big difference between "undefined" behavior and "implementation-defined" behavior.

For example, there's a lot of spooky "undefined behavior" around dereferencing pointers. In one famous case [1], dereferencing a pointer actually led the compiler to skip a later check on whether the pointer was NULL, because if the pointer was already dereferenced then it must have been valid.

Another classic "implementation-defined" detail is what is the size of a "char"? Nowadays we can readily assume it's 8 bits, but that wasn't so guaranteed when C was written!

[1] https://lwn.net/Articles/342330/

Re: The Descent to C (2013)

#70
post #55

> (In fact, that's what array[i] means – the language defines it to be a synonym for *(array+i).) To really drive home the primitiveness of C arrays, should probably also mention that, because addition is commutative, you could also write i[array] and somewhat surprisingly, it will compile, and work, and it means "*(i + array)" which is equivalent to "*(array + i)" But nobody really does that, because that would be k…

This is actually a really good way to drive home that arrays don't really "exists" in C and are just syntaxic sugar for pointer arithmetic.

They exist in the sense that their length is sometimes known if the full definition is visible. sizeof(array) isn't the same as sizeof(generic ptr to array).
Post reply on HN