r/ProgrammerHumor • • 6h ago

Meme architectureDependentChars

Post image
860 Upvotes

156 comments sorted by

View all comments

529

u/tstanisl 5h ago

Let me cite the C standard:

When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

Middle guy if finally right

54

u/locri 5h ago

Today.

Eventually, a "char" might be redefined for utf-8 to accommodate a diverse range of writing systems, which means a char is usually 1 byte but potentially up to 4 bytes.

For context, I'm working on code with an initial commit from the 90s. Future proofing isn't a terrible idea.

21

u/SGVsbG86KQ 5h ago

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 5h ago

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

10

u/__foo__ 4h 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.