-4

I'm not sure how to unlock characters past 255 automatically.

char f;
f = 'k';
printf("%d\n", f);

Results in 107. No surprise.

f += 500;
printf("%d\n", f);

Results in 95. It seems this has been modulo divided by 255.

printf("%c\n", 607);

Results in _. There are many more characters that extend into the thousands as well. How come adding a value and making a char go past 255 forces a modulo operation? I need to go past 255 for the sake of a hash function.

Synlar
  • 101
  • 6
  • 2
    `sizeof(char)` is one byte. So it could not be greater than 255. If you need more, use `wchar_t` or its analogs. – Ari0nhh May 13 '16 at 11:53
  • 3
    I strongly encourage you to read this: [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – bolov May 13 '16 at 11:55
  • 4
    @Ari0nhh It's true that `sizeof(char)` is one, it's not necessarily true that it *can't* be greater than 255. `sizeof(char)` can be 1 but it *could* consist of 16 bits. – P.P May 13 '16 at 11:55
  • 1
    Just go read chapter one of a C programming book. That being said, you shouldn't use `char` to store numbers for other reasons, namely the implementation-defined signedness. – Lundin May 13 '16 at 11:57
  • 1
    @P.P. In C, yes, but POSIX mandates byte, thus `char` to be exactly 8 bits. – el.pescado - нет войне May 13 '16 at 11:57
  • @Lundin I need to enumerate them for a hash function and they will need to be a huge range of characters after reconversion. Admittedly the assignment question this is from is terrible but unfortunately marks have to be obtained. – Synlar May 13 '16 at 12:00
  • It seems that in your case, `char` is `unsigned` and its range is 0-255. When it goes beyond 255, the value wraps-around which is the same as mod 256 – Spikatrix May 13 '16 at 12:01
  • 2
    @el.pescado: A "byte" is not defined to be 8 bits. POSIX might define a byte in the standard to have 8 bits, but the reverse is not true. Anyway, C is not POSIX and there is no indicator the question is about POSIX. The C standard only uses `char` and "byte" synonymously, but does not enforce the bitwidth - that's why `CHAR_BIT` exists. – too honest for this site May 13 '16 at 12:05

2 Answers2

3

A char is a byte in memory. So you can not store values > 255 because it has only 8 bits (assuming that on your platform a char is defined as a byte of 8 bits). When the value is more than 255, it overflows.

Furthermore, you don't know if a char is unsigned (values can be between 0 and 255) or signed (values can be between -128 and 127).

You can use another type than char if you want to store values > 255.

blatinox
  • 833
  • 6
  • 18
0

A char data type is an 8-bit field, with a maximum total value of 255. You need to use a 16-bit value if you wish to work with numbers greater than 255.

unsigned char has a value range of 0 to 255
signed char has a value range of -128 to 127

BackDoorNoBaby
  • 1,445
  • 2
  • 13
  • 20