unsigned char c = UCHAR_MAX; // e.g. 65'535 for CHAR_BIT = 16
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is line that feels wrong for me.
CHAR_BIT = 16 is a corner case, I don't even understand why it is allowed by the standard.
unsigned char c = UCHAR_MAX; // e.g. 65'535 for CHAR_BIT = 16
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is line that feels wrong for me.
CHAR_BIT = 16 is a corner case, I don't even understand why it is allowed by the standard.
For historical reasons the original C types bit-width were not strongly defined as most hardware of the time had "exotic" registers and addresses by today standards.
But it was a long time ago. It is reasonably safe to assume that 14bits ints and 21bits pointers are deprecated.
Why would DSPs would need to change the bit-width of chars? We already have shorts and ints that are better candidates for 16bits and 32bits width.
I would not mind if the C standard was made both stricter and simpler, and I maintain that the author is wrong, fputc (and all other IO libs depending on it) should not be modified to take into account this kind of absurd corner cases.
Doing so will introduce more bugs and complexity that would have to be maintained.
A byte type that is always 8bits would be a better approach, i think. But c++ bungled this too, it is the size of a char still. I dont think it should exist on systems that cannot address 8bits
Slower, maybe, but saner and conformant to a pre-existing standard.
So if a DSP cannot address bytes separately, but it can address 16-bit words, the size of a "char" must be 16 bits. This is independent of choosing to define "short" as a 16-bit integer.
It is useful to have a type corresponding to the smallest addressable part of the memory, but it should not have been named "char", which is a name that should have been reserved for ASCII or UTF-8 data, and never used for numbers or bit strings.
This is beyond ridiculous.
Although, I guess the bigger problem is to how to represent pointers to 8-bit values - on Alpha a pointer with value 1 would point to the octet at offset 1 in memory, where presumably on this 16-bit DSP such 1-pointer would point at the octet at offset 2.
- cread and cwritten are always the same, which is required by the standard
- they differ from c
Meaning fputc does return the value actually written, not the value input.
And OP spends the entire essay freaking out about that. Well really that fputc would operate on octets when the implementation’s char is larger than that. You can see it in the second section, TFA complains that TI / DSPs return 2. Which, mind, is the entire point of fputc returning what it wrote.
That is what I would think obvious here, as it's generally how char-based functions operate, and doing CHAR_BITS IO would be a complete mess for portable IO (and would introduce ambiguities due to endianness).
Although technically what it says is:
> The fputc() function shall write the byte specified by c (converted to an unsigned char) to the output stream pointed to by stream
So if you have CHAR_BITS=16 and WORD_BIT=16 things get dicey. But indeed the easiest way to resolve the ambiguity is to assume that like everywhere else it only operates on the "minimum" char boundaries (an octet), and `int` is only used to allow or return `EOF`. This is what usually occurs in other char-based functions.
Though in all honesty I don't know how a platform with CHAR_BITS=16 handles `char*` / `char[]`.