Name: Anonymous 2016-01-08 14:23
https://matt.sh/howto-c
Very good read for anyone that wants to write this outdated language in 2016.
Very good read for anyone that wants to write this outdated language in 2016.
Developers routinely abuse char to mean "byte" even when they are doing unsigned byte manipulations. It's much cleaner to use uint8_t to mean single a unsigned-byteHow is "uint8_t" cleaner than a simple and elegant "char"? These faggots desperately need a renaming of all the standard types. In particular, getting rid of the "_t" bullshit is imperative.
char
is unsigned, therefore you can get all sorts of undefined behavior that way. stdint.h
promotion seems to be baseless and useless: there's solid reasons for using char
/int
/unsigned
(also, they wouldn't be in the standard anymore if they were 'obsolete'/dangerous, like gets()
).size_t
(although I try to avoid it) and -Wall -Wextra
, though. It's an accident of history for C to have "brace optional" single statements after loop constructs and conditionals. It is inexcusable to write modern code without braces enforced on every loop and every conditional. Trying to argue "but, the compiler accepts it!" has nothing to do with the readabiltiy, maintainability, understandability, or skimability of code. You aren't programming to please your compiler, you are programming to please future people who have to maintain your current brain state years after everybody has forgotten why anything exists in the first place.
solid reasons for using char/int/unsignedWhat are they?
char
is CHAR_BIT
, C standard requires it to be at least 8 bit; POSIX mandates CHAR_BIT == 8
char
s. char
is always:able to hold a decimal value from 0 to 127And if you don't see the implications of this, then just continue using
able to hold 8 binary digits
able to hold an ASCII character (ANSI, too, but that's a little more complicated whenCHAR_BIT
isn't 8)
of size 1
uint8_t
. int
is at least 16 bits wide, it is more ideal in that situation to use uint_fast16_t
or uint_least16_t
depending on whether you are iterating or storing in an array.Not all platforms implement uint8_tOK: http://www.cplusplus.com/reference/cstdint/
UINT8_MAX
is defined to be 255, so uint8_t
(or whichever of uint_fast8_t
or uint_least8_t
is chosen if it does not exist) is still more useful than char
. Refer back to >>27.