← Back to context

Comment by quelsolaar

14 hours ago

Good article.

C would probably not have survived unless it had this flexibility.

But its not justa historical thing. Today there are modern platforms like DSPs that have 32bit sized char, because that is the smallest addressable type. These platforms depend on C for tool chains, even if most "portable" C wont run correctly on them. The fact that you can build hardware like that, and not have to invent a new language / dialect to program them is a huge win for the world.

<edit> I didnt see the footnote about DSPs at first read </edit>

This kind of flexibility is a purely historical thing.

On modern computers, it is impossible to write correct C programs that are agnostic about the true size in bits of the "flexible" types char, short, int, long and long long.

If your program must depend on assumptions about the size in bits of the integer types, those assumptions must be made explicit, by using types like int16_t, int32_t etc.

Writing correct programs that are agnostic about the integer sizes is possible only in programming languages that allow the programmer to install an integer overflow handler even if the CPU does not generate a hardware exception for that, in which case the compiler must insert appropriate overflow checking instructions that would invoke the installed handler when necessary.

This problem did not exist on old computers, where there were hardware exceptions for integer overflows, so even in C you could install a signal handler for SIGFPE, which would also be invoked by integer overflows.

  • That's not true. All the basic types have minimum sizes, so its perfectly reasonable to write software that stays within those bounds. You can avoid any overflows if you know the minimum limits.

    • If you use the minimum sizes, your program will be very inefficient on most CPUs.

      I have never considered that this is an acceptable solution, which is why in decades of using C, during which I had many times to port programs or even entire real-time operating systems between different ISAs, I have never used those minimum sizes.

      Instead of having those minimum sizes, which I consider useless, C should have had since the beginning, besides sizeof, which gives the size ratio between another type and char, another operator or macro to provide the size in bits of any integer type.

      With that, it would have been possible to use types like short, int or long in a portable way.

      Nowadays, there are _WIDTH, _MAX and _MIN constants for the integer types, but those have been added relatively late to the language, together with the integer types with specified width, for which those constants are superfluous.

      1 reply →

C survived because UNIX carried it.

  • I have had the suspicion for a while now, that any attempted discussions about the merits of C's design that try to connect this to its success, are missing a massive part of the picture if they don't mention UNIX.

    We don't make this mistake about JavaScript; nobody tries to argue that the confusing semantics of the "var" keyword or the "==" operator somehow were actually instrumental to its success. It's obvious that JavaScript won because it was what was on the web. Maybe C is not much different.

  • I don't agree. UNIX was an extremely niche operating system until Linux won the data center, at that time both C and C++ were already extremely popular outside the UNIX world. C won because it was so easy to adapt to new hardware architectures (even GPU shading languages are just minimally extended flavours of C and C++).

    • > UNIX was an extremely niche operating system until Linux won the data center

      UNIX was ubiquitous in the data center well before Linus even posted his first version of Linux on USENET.

      Everything ran on SunOS, HP-UX, IRIX, AIX, etc.

    • Most people only cared about C, because they needed to work on UNIX, and UNIX was taking over the server room and all 1980's graphical workstations.

      C was pretty much ignored on 8 bit home computers, outside some toy compilers for CP/M.

      In the 16 bit days, it was yet another language alongside BASIC compilers, Pascal, Modula-2, Assembly.

      C is so tied to UNIX, that POSIX had to be created so that any non-UNIX operating system could provide a cozy home for their C compilers.

      UNIX/POSIX is for all practical purposes the runtime most C applications rely on, there are naturally some exceptions like free-standing or Windows (which eventually gave up and add to start improving its support).

      It is only due to historical accident that Microsoft gave up on Xenix, instead of replacing their MS-DOS efforts.

      1 reply →

    • > UNIX was an extremely niche operating system until Linux won the data center

      ?!? What a claim!

    • Sorry, what? Most of data centers were running a UNIX operating system back in the day. What operating system do you think they were running?

      8 replies →

    • Disclaimer: This is just some cursory research using LLMs.

      C was invented to rewrite UNIX in a programming language that made it easy to port UNIX between machines.

      So what you're saying is contradictory. You're saying the underlying motivation of C was wrong or unnecessary (porting UNIX to different hardware architectures) but C won because that underlying motivation (easy porting between hardware architectures) was partially right.

      Your position is now that C didn't need UNIX as a stopgap, which is weird because your argument gains no weight (basically saying C's dominance is sheer coincidence) if it's true but if it's false you're just plain wrong.

      6 replies →

Not sure why you would need to invent an new programming language when we're still strictly talking about data types. You just introduce a new data type for the hardware if that's what's necessary. You don't need a whole language.

This is why I think so many C developers have no clue what they are doing. They just take whatever decision was made in C as gospel instead of thinking of everything being up for negotiation.