r/ProgrammerHumor • • Oct 04 '26

Meme architectureDependentChars

Post image
3.0k Upvotes

372 comments sorted by

View all comments

Show parent comment

29

u/SGVsbG86KQ Oct 04 '26

No that's not how that works. Even if char would be 32 bits, sizeof(char) is still defined to be 1.

3

u/Jbolt3737 Oct 04 '26

Does that make sizeof(int) equal 1, or does it make an int 128 bits?

16

u/__foo__ Oct 04 '26

IIRC the only requirement the C standard makes for int is that it needs to be at least 16 bit wide. Everything else is up for the compiler developers to decide. If a char and int were both 32 bit wide both would be sizeof = 1. If the compiler makers decide it would be a sensible idea to have a 128 bit int it would be sizeof = 4 in this case.

1

u/conundorum Oct 06 '26 edited Oct 06 '26

Generally, the requirements are usually in terms of data ranges and not actual bit counts. E.g., unsigned int is defined as "large enough to hold 216-1", which means that sizeof(unsigned) (and by extension, sizeof(int)) would be 1.

Here's a quick reference:

  • Signedness: All signed types must be the same size as their unsigned counterpart. char is the same size as its unsigned & signed counterparts.
  • bool: I hear it's pretty lean.
  • unsigned long long: Can hold 264-1.
  • unsigned long: Can hold 232-1.
  • unsigned int: Can hold 216-1. (De facto upgraded to 232-1, since almost everything has 32-bit ints.)
  • unsigned short: Can hold 216-1.
  • unsigned char: Can hold 28-1.
  • Byte: Is sizeof(char), and must have CHAR_BIT bits.

(Yes, bytes are defined in terms of char, and not the other way around. It kinda takes you by surprise the first time you find out that C/C++ bytes are 64 bits on any platform with 64-bit char, regardless of what the architecture says.)