Re:Writing C software without the standard library
by DSMan195276 on Tuesday November 29, 2016 @02:25PM
(#13065537)
Well there's a few things to keep in mind. For one, `sizeof(char)`is guarenteed to be `1` - the standard simply says so. That doesn't mean that a `char` is 8-bits long, however. On certain systems, like TI DSP's, `char` is 16-bits long, and thus `sizeof(short)` is also 1. `sizeof(int)` may be either 1 or 2, I can't recall (both are standards compliant).
All that said, POSIX requires `CHAR_BIT == 8`, which then basically ensures what you wrote to be true. So if you're willing to target POSIX (or POSIX-supporting systems) then such a thing is perfectly fine.
The `stdint.h` types are still better though, IMO, because they make your intentions a lot more clear.
Re:Writing C software without the standard library
by jblow on Tuesday November 29, 2016 @01:30PM
(#13065046)
I think int is 8 bytes on the PS4. (I just got bitten by this...)
Re:Writing C software without the standard library
by I_deny_it on Tuesday November 29, 2016 @08:40PM
(#13067748)
How does the PS4 declare a 4 byte int? If that's "short", is there a way for a 2 byte int?
Re:Writing C software without the standard library
by DSMan195276 on Thursday December 01, 2016 @03:09AM
(#13077993)
I haven't programming on the PS4, but an 8-byte `int` sound very suspect and fairly unlike (but not impossible). That said, it wouldn't be a huge issue. `short` could either be a 2-byte or 4-byte int (Either would be standards compliant), and `char` would presumably still be byte-sized (Not doing so would be a fairly big issue to deal with).
That leaves out either the 2-byte or 4-byte int from the standard data-types, but you can gain that back by simply using a compiler attribute or compiler-defined type to allow access too it. While that sounds non-standard, it really wouldn't be that bad because it could simply be used in `stdint.h` to expose the standard `int16_t` and `int32_t` types, which could be used like normal.
And for what it's worth, using typedefs based on the above provides more readable printf strings. This is hideous:
Where this is acceptable: