4 ms·
I had an online discussion some years back where I suggested that C nail the size of char to 8 bits. He responded that there was a CPU that had chars be 32 bits
by WalterBright 5y ago
I had an online discussion some years back where I suggested that C nail the size of char to 8 bits. He responded that there was a CPU that had chars be 32 bits, and wasn't that great that a C compiler for it would be Standard compliant?
I replied by pointing out that nearly every non-trivial C program would have to be recoded to work on that architecture. So what purpose did the Standard allowing that actually achieve?
I also see no problem for a vendor of a C compiler for that architecture making a reasonable dialect of C for it. After all, to accommodate the memory architecture of the x86, nearly all C compilers in the 80's adopted near/far pointers, and while not Standard compliant, it really didn't matter, and was tremendously successful.
D made some decisions early on that worked out very well:
1. 2's complement wraparound arithmetic
2. sizes of basic integer types are fixed at 1 byte for chars, 2 for shorts, 4 for integers, 8 for longs. This worked out very well
3. floating point is IEEE
4. char's are UTF-8 code units (*)
5. chars are unsigned
These 5 points make for tremendous simplicity gains for D programmers, and ironically increase portability of D code.
After reading the paper, I'm inclined to change the definition of UB in D to not mean it can be assumed to not happen and not be unintended.
(*) thanks for the correction
- WalterBright 5y agoI had another such discussion where I suggested that C abandon support for EBCDIC. I was told it was great that C supported any character set! I said C certainly does not, and gave RADIX50 as an example. How many C programs today would work with EBCDIC? Zero? There's no modern point in C not requiring ASCII, at a minimum.
- sedatk 5y ago> I was told it was great that C supported any character set I'm all for extensibility mechanisms to shut down the people like this. You want EBCDIC? It's now a github repository; go ahead and maintain it, or shut up.
- slavik81 5y agoWhat is it about RADIX50 that makes it incompatible with the C standard? The lack of a reserved null value?
- WalterBright 5y agohttps://en.wikipedia.org/wiki/DEC_RADIX_50 https://en.wikipedia.org/wiki/DEC_RADIX_50 Note the lack of lower case, characters like { } ( ) [ ] * : & | # !, and on and on.
- slavik81 5y agoOh, as a source file encoding. Well, it lacks the characters needed for trigraphs, so maybe we could add support with a quadgraphs extension...
- gpderetta 5y agoFWIW, C++ dropped support for EBCDIC when it dropped trigraphs. IBM complained till the last minute.
- tialaramex 5y ago> char's are UTF-8 code points I'm guessing you mean that char is a UTF-8 code unit as you keep saying they're only one byte and a code point is far too large to fit in a byte / octet. But that still seems very weird because a UTF-8 code unit is almost but not quite the same as a byte, so that users might be astounded when they can't put a byte into a char in this scheme (because it isn't a valid UTF-8 code unit) and yet almost no useful value is accrued by such a rule.
- WalterBright 5y agoAh, code unit is correct. I am always getting code unit and code point conflated. Sorry about that. > a UTF-8 code unit is almost but not quite the same as a byte D dodges that problem by having `byte` for signed 8 bits, and `ubyte` for unsigned 8 bits. That gets rid of the "is char unsigned or signed" confusion and bugs.
- tialaramex 5y agoAha, so if my D code doesn't care about text at all, I only need to worry about ubyte (or if I want them signed, byte) and char is irrelevant to me. That makes some sense. Still it seems weird to me to separate the concerns "This byte isn't a valid UTF-8 code unit" and "This string isn't valid UTF-8". I don't know what flavour of error handling D has, but I can't conceive of too many places where I want to handle those cases differently, so if they're separate I have to do the same work twice. Also, one very natural response to invalid UTF-8 is to emit U+FFFD (the replacement character �) but of course that doesn't fit into a UTF-8 code unit, so an API which takes a ubyte and gives back a char must error.
- WalterBright 5y agoI think you'll find that once you get used to the idea that char is for UTF-8, and ubyte is for other representations, it not only feels natural but the code becomes clearer. As for invalid code points, you get to choose the behavior: 1. throw exception 2. use the replacement char 3. ignore it All are valid in real life, and so D is accommodating.
- jeffbee 5y agoI don't think it would be a very good outcome if people forked C such that everyone working on DSP platforms and new platforms that you just haven't heard of had to use a fork with flexible CHAR_BIT while the standard defined it to be 8. Who is served by this forking? Plenty of software works fine with different CHAR_BIT values, although some poorly-written programs do need to be fixed.
- WalterBright 5y agoIt'd almost certainly have to be a fork anyway. > Plenty of software works fine with different CHAR_BIT values, Most won't. And good luck writing a diff program (for example) that works with CHAR_BIT == 32. > although some poorly-written programs do need to be fixed. Blaming all the problems on poorly-written programs does not work out well in real life. Good language design makes errors detectable and unlikely, rather than blaming the programmer.
- jeffbee 5y agoSomehow it never occurred to me to run `diff` on a SHARC. Horse for courses.
- astrange 5y ago> Plenty of software works fine with different CHAR_BIT values, although some poorly-written programs do need to be fixed. Since C traditionally doesn’t have much of a testing culture I’m not sure you can trust any decent sized project here. I’d even be surprised if you could change the size of ‘int’. And moving off 2s complement is definitely out.
- WalterBright 5y ago`long` is such a mess in C I never use it anymore. I use `int` and `long long`. `long` is so useless today that it's a code smell and the Standard should just delete it.
- 5y ago
- layer8 5y ago> So what purpose did the Standard allowing that actually achieve? I believe the situation was that there were C implementations for DSPs (32-bit-addressable only) and IBM mainframes (36-bit addressable only), and when ANSI/ISO C was established, they naturally wanted their implementations to be able to conform to that new standard. So the standard was made flexible enough to accommodate such implementations. Similarly why signed overflow is undefined behavior. There were existing implementations that trapped (CPU interrupt) on signed overflow. I might have gotten the details wrong, but that's what I remember from reading comp.std.c (Usenet) in the 90s.
- arunc 5y ago> After reading the paper, I'm inclined to change the definition of UB in D to not mean it can be assumed to not happen and not be unintended. What's the current definition of UB in D?
- WalterBright 5y agoSame as in C.
- inkyoto 5y ago> I had an online discussion some years back where I suggested that C nail the size of char to 8 bits. He responded that there was a CPU that had chars be 32 bits, and wasn't that great that a C compiler for it would be Standard compliant? Back in C infancy days, there had existed architectures where a byte could hold 9 bits that C compilers had to be written for. The 36-bit PDP-10 architecture springs to mind, and some Burroughs or Honeywell mainframes had those – I remember reading a little C reference book authored by Kernighan, Ritchie and somebody else explicitely calling out the fact that a C implementation could not rely on the fact of the byte always being 8 bits long and also stressing that the «sizeof» operator was reporting the number of bytes in a type irrespective of the bit width of the byte. 9 bit byte architectures have all but perished, however, C has carried the legacy of creative days of the computer architecture design along.
- WalterBright 5y agoI know. I've programmed on 36 bit machines (the amazing PDP-10), and machines with 10 bit bytes (Mattel Intellivision). But that was over 40 years ago. Time to move on.