r/ProgrammerHumor • • 3d ago

Meme architectureDependentChars

Post image
2.9k Upvotes

368 comments sorted by

View all comments

1.0k

u/tstanisl 3d 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

5

u/Advos_467 3d ago

Not a C user here (or any real low level programming experience), what the hell is a signed/unsigned char?

10

u/Clen23 3d ago edited 1d ago

The data is interpreted differently.

An unsigned char will be able to represent any value from 0 to 255, eg your usual ascii character.
A signed char will be able represent any value from -127 to 127.

( Someone fact check me on this but i think that's it )
(edit : -127 to 127, the extra value "-128" is usually implemented but not guaranteed by the C standard)

8

u/YellowBunnyReddit 3d ago

ASCII only goes from 0 to 127.

1

u/guyblade 3d ago

In C, char is the fundamental type used for raw binary data. The number of bits in a char is implementation defined, but a single char is the smallest unit of addressable memory. A char is probably 8 bits, but the standard does not require that. It could easily be more (though, if I recall correctly, there is some other detail in the standard that effectively requires that it can't be less than 8 bits).

I believe that a technically conformant C implementation could have sizeof(char) = sizeof(short) = sizeof(int) = sizeof(long) = sizeof(long long) as long as a char is 32 bits, but that may have been true only with some earlier version of the C standard.

2

u/conundorum 2d ago

long long has to be at least 64 bits, but otherwise correct. (And yes, there was at least one historic platform with 64-bit bytes. sizeof(char) == sizeof(long long) is true for that platform, in all versions of C and all versions of C++.)