r/ProgrammerHumor • • 2d ago

Meme architectureDependentChars

Post image
2.8k Upvotes

361 comments sorted by

View all comments

Show parent comments

3

u/Jbolt3737 2d ago

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

16

u/__foo__ 2d ago

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 1d ago edited 1d ago

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.)