4 ms·
I'm going to speculate a bit on why these silly things are in C. C was developed on a PDP-11 that had 64Kb of memory. That's not much of any at all. Therefore,
by WalterBright 2y ago
I'm going to speculate a bit on why these silly things are in C.
C was developed on a PDP-11 that had 64Kb of memory. That's not much of any at all. Therefore, the compiler must be extremely tightly coded.
The fundamental rules of the C language are pretty simple. But articles like these expose consequences of such simple rules. Fixing them requires adding more code. Adding more code means less room for the code being compiled.
Therefore, if the intended use of the language works, the pragmatic approach would be to simply not worry about the quirky consequences.
A more interesting question would be "why do these characteristics persist in modern C compilers?"
The stock answer is "backwards compatibility", "Obfuscated C Code contests" and "gotcha job interview questions". My argument would be that there is no reason for the persistence of such "junk DNA" and it should be deprecated and removed.
I've done my part. D doesn't support that stuff, even though the basic use of the language is easily confused with C.
For example:
#include <stdio.h>
void main()
{
int i;
for (i = 0; i < 10; ++i);
printf("%d\n", i);
}
I've died on that hill. I know others who lost an entire day staring at it wondering what's wrong with it. I saw it on X recently as "99% of C programmers will not be able to find the bug."
The equivalent D code:
import core.stdc.stdio;
void main()
{
int i;
for (i = 0; i < 10; ++i);
printf("%d\n", i);
}
gets you:
test.d(5): Error: use `{ }` for an empty statement, not `;`
C'mon, Standard C! Fix that!
- HeliumHydride 2y agoI was able to find it instantly, but that's because I always use curly braces for my if/while/for loops.
- WalterBright 2y agoI added a warning for it in my C compiler back in the mid 1980s. Fun story. A friend of mine (Eric Engstrom!) bought himself a backhoe. I'd never driven one before and he offered to let me drive it. Sure! The clutch pedal works backwards from that in a car. Press on the clutch to engage it, release the pedal to disengage it. After some struggling with my reflexes being all wrong, I came within a couple feet of taking out the side of his barn - by switching off the key. There was nothing wrong with that user interface, other than being insane.
- binaryturtle 2y agoIndeed, enforcing brackets here would be the proper fix, IMHO. :) I always use brackets too. That's simply a less error-prone style.
- moefh 2y ago> I know others who lost an entire day staring at it wondering what's wrong with it. I saw it on X recently as "99% of C programmers will not be able to find the bug." Both gcc and clang give a warning[1] for that code with just "-Wall", so I's hard to imagine it being a real problem these days. [1] https://godbolt.org/z/vfPzhc596 https://godbolt.org/z/vfPzhc596
- WalterBright 2y agoI know modern compilers do it, too. But still the language needs to be fixed. The proof is C programmers still get victimized by this useless feature. Compiler warnings are a good source of material for things that need to be fixed in the language. Unfortunately, every compiler has their own set of warnings, and sometimes warnings from different compilers contradict each other. That encourages programmers to not use the warning feature. That's another reason why the language should be fixed.
- uecker 2y agoIn my experience the warnings work quite well for the programmers I know. Anyway, ranting on HackerNews does not get anything fixed: https://www.open-std.org/jtc1/sc22/wg14/www/contributing.html https://www.open-std.org/jtc1/sc22/wg14/www/contributing.htm...
- pjmlp 2y agoWorking in offshoring projects for the lowest bid changes one's point of view, regarding "programmers I know" approach.
- uecker 2y agoI work a lot with students who can not program well in C. I would say turning on -Wall is not a difficult problem for them. If it is, then using another programming language also does not help.
- 2y ago
- WalterBright 2y ago> I've died on that hill. But I'm feeling much better.
- dfawcus 2y agoclang and gcc now warn of that. $ gcc-12 -g -O2 -std=c11 -Wall -Wextra -Wpedantic -Werror c-error.c c-error.c:2:10: error: return type of ‘main’ is not ‘int’ [-Werror=main] 2 | void main() | ^~~~ c-error.c: In function ‘main’: c-error.c:5:9: error: this ‘for’ clause does not guard... [-Werror=misleading-indentation] 5 | for (i = 0; i < 10; ++i); | ^~~ c-error.c:6:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’ 6 | printf("%d\n", i); | ^~~~~~ cc1: all warnings being treated as errors and: $ clang-14 -g -O2 -std=c11 -Wall -Wextra -Wpedantic -Werror c-error.c c-error.c:2:5: error: 'main' must return 'int' void main() ^~~~ int c-error.c:5:33: error: for loop has empty body [-Werror,-Wempty-body] for (i = 0; i < 10; ++i); ^ c-error.c:5:33: note: put the semicolon on a separate line to silence this warning 2 errors generated. Now granted, those are specific implementations, not things mandated by language changes.