These examples illustrate well-intended and useful features of C, not flaws. I will explain why for each in a comment below:
These examples illustrate well-intended and useful features of C, not flaws. I will explain why for each in a comment below:
You mean Algol, Mesa, and few others from the same vintage or even older?
Or rather Macro Assemblers like MASM, TASM that provided higher level macros for structured programming?
Algol is higher level than C.
Sure, but it is also safer and already had helped implementing a few operating systems before C's authors could imagine coming up with C.
My understanding of history is compiled Algol wasn't nearly as fast as compiled C, which was needed for operating systems and performance-critical code.
Edit: Forgot to mention that up to the early 90's, C compilers generated pretty crappy code vs what any average Assembly coder could write. And was only relevant for those fortune to have UNIX at their company or university.
What do you think was the reason C took off while Algol use diminished? Was the growth of Unix a significant reason? Do you think C's adoption was misguided?
I think C's took off because C fit the sweet spot of ability to produce fast code while still being cross-platform and human-readable.
As those workstations became a success in the US market, its use spread outside US and the need to have developers that could write software for them increased. This meant knowing C.
All the other operating systems at the time didn't offer C compilers. The few that did, it was just another language to choose from, most of the time only a subset of K&R C.
This is how the distinction between libc and POSIX APIs came to be. The original libc is mostly what could be implemented in other OSs without depending directly from the UNIX API semantics.
If the likes of Sun and SGI hadn't succeeded, most probably C would be a footnote just like Algol.
I have been writing software since 1986 and 1992 was the first time I cared to learn C, just to quickly ditch it for C++ on the year thereafter.
#2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions such as asserts or crashes to guard against potentially dangerous side effects. C compiler assumes that the programmer is in control of his/her code. In this example, it can be assumed that a careful C programmer has already guaranteed that the pointer will not be null when it is dereferenced. This C feature is an optimization to avoid generating redundant or unnecessary asserts or handling code.
3. C allows the programmer to handing pointers, allowing for such low-level optimizations that may not be possible in higher-level languages. A careful C programmer may have taken steps outside the function to handle the situation where yp==zp, or may otherwise be unconcerned about a particular case, for performance reasons.
4. A correct implementation of IEEE 754.
5. Since C is designed to efficiently compile to any computer architecture, it needs to be aware of the distinction between the arithmetic width of an instruction set vs the width of addresses. Ints are optimized to default to the natural arithmetic width of an instruction set (so that compilation doesn't produce unnecessary packing/unpacking instructions whenever they are accessed) but is guaranteed to be atleast 16 bits wide. However, since data structure can be as large as addressable memory, it is necessary for size_t to be the width of addresses.
6. Allowing size_t to be unsigned allows all bits of a size_t variable to be utilized for expressing size.
7. Undefined behavior is, again, an optimization feature, allowing each compiler to implement as it sees fit.
8. Comma operator is useful when first operand has desirable side effects, such as compactly representing parallel assignment or side effects in for loops.
9. C allows unsigned integers to wrap around 0 and UINT_MAX. This feature can be utilized as an optimization, for example as a free (no additional instruction) deliberate modulus operation. This is usually how unsigned integers behave in assembly.
10 & 11 & 12. Some ISA's, like MIPS, treat overflow of signed numbers as an exception. Others simply treat the result as a valid two's-compliment value. Since C is machine independent, C's official specification for overlow of signed numbers must be compatible for all ISA's. Simply treating the result as undefined does the trick, and means the compiler doesn't have to make guarantees or version for each ISA.
One disadvantage of making int 64 bits, even if that's the natural size, is that if char is 8 bits, then short has to be either 16 or 32 bits (or 64) -- which means that you can't have predefined types covering all the common sizes (8, 16, 32, 64).
That's not quite true, since C99 introduced extended integer types -- but I don't know of any C compiler that has provided them.
(The intN_t and uintN_t types in <stdint.h> don't solve this; they still have to be defined in terms of existing types.)
6. size_t is required to be unsigned.
9. C requires wraparound behavior for unsigned integers.
10, 11, 12. It's not just the result of an overflowing signed integer arithmetic operation that's undefined, it's the behavior. `INT_MAX + 1` can yield `INT_MIN`, or it can yield 42, or it can crash your program and reformat your hard drive (at least in principle).