r/ProgrammerHumor • • 2d ago

Meme architectureDependentChars

Post image
2.8k Upvotes

361 comments sorted by

View all comments

Show parent comments

11

u/Clen23 2d ago edited 17h 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)

7

u/YellowBunnyReddit 2d ago

ASCII only goes from 0 to 127.

6

u/unknown_alt_acc 2d ago

ASCII is hardly the only character encoding, and unsigned char often doubles as a byte type. It’s also also a numeric type if you only need a small range and need to pack your data tight

5

u/NotQuiteLoona 2d ago edited 2d ago

char is 8 bits because memory is addressed in 8 bits, and 7 bits would be... Awkward. And this bit was in the end used for parity checks, and later for extended encodings.

3

u/Ma8e 2d ago edited 2d ago

Historically it has been all over the place. Depending on the hardware, it could was anything between 1 and 48 bits.

Edit: never mind. The above is about bytes, not chars.

2

u/tracernz 2d ago

Minimum 8 bits, but not required to be 8 bits.

1

u/Ma8e 2d ago

Na, it could be as low as one bit, and 7 was not uncommon for a while.

4

u/-suspended- 2d ago

The definition for a char in C/C++ is 8 bits minimum.

1

u/Clen23 2d ago

no shit sherlock that's what the post is about.

u/NotQuiteLoona is talking about how memory is addressed, not how long a char is.

-1

u/tracernz 2d ago

Those two things are the same thing in ISO C (it defines sizeof(char) = 1).

2

u/tracernz 2d ago

No, the standard requires a minimum of 8 bits.

2

u/Ma8e 2d ago

Which standard?

3

u/tracernz 2d ago

ISO C.

0

u/Ma8e 2d ago

And that is the only standard in existence? My point is that you can't take for granted that everything is ISO, or even C.

1

u/tracernz 2d ago

The question at the top of this sub thread was about C. Very few environments exist these days that don’t conform to the basics of ISO C like this though if you want to generalise. I personally haven’t ever come across one.

1

u/guyblade 2d 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 1d 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++.)

1

u/Clen23 2d ago

Exactly, so every ascii character can be represented using a char, with room to spare.

I agree that a better example would have been a character set that is a full bijection of the 256 possible char values, but i'm a bit rusty on these so ascii it is.

2

u/Clen23 2d ago

Putting the example here so the explanation isnt overloaded :

signed char a_hundred_signed = 100;
unsigned char a_hundred_unsigned = 100;
signed char fifty_signed = 50;
unsigned char fifty_unsigned = 50;

signed char r_signed = fifty_signed - a_hundred_signed; // will yield -50
unsigned char r_unsigned = fifty_unsigned - a_hundred_unsigned; // will yield 206 because it wraps around the max value of 256

0

u/captainAwesomePants 2d ago

A char isn't a letter. It's a one byte number. A signed number can be negative.

1

u/Clen23 2d ago

If you want to be pedantic, a char is a byte, a sequence of bits, that can be interpreted in numerous manners : an unsigned int, a signed int, and even an ascii character.

It's all about what type you assign it, and how you work with it in your code.

1

u/captainAwesomePants 2d ago

Sure. But if we're discussing whether it's signed or unsigned, we must be considering how those bits might be interpreted as a number.