1

I am aware that C++ has 3 types of char variables.

char // with range : -127 to 127 or 0 to 255 ??
signed char // with range : 0 to 255
unsigned char // with range : -127 to 127

Correct? So which one is it actually for char?

My problem is that I want to use istream::read function which requires a * char parameter. However, I know the values my program will be reading will be in range 0 to 255.

How do I know that if I do this file.read((char *)buffer, size);, I will be guaranteed that every single thing being read will not be set to a value with a range of -127 to 127 ?

edit: I understand that my char can be both ranges depending on my platform/ implementation. Correct? If yes, how can I know which platform/ implementation I am using ? Does it depend on my IDE- compiler ? OS?

Marios Ath
  • 618
  • 2
  • 9
  • 21
  • 5
    It's implementation-defined whether `char` is signed or unsigned. There's probably a dupe around... – Max Langhof Nov 29 '18 at 18:00
  • For 2-complement it is not -127 but -128. And usually compilers have a switch which determines whether a char is signed or unsigned. – geza Nov 29 '18 at 18:01
  • "How do I know that if I do this file.read((char *)buffer, size);, I will be guaranteed that every single thing being read will not be set to a value with a range of -127 to 127 ?" - Without consulting the documentation for your specific implementation, or checking CHAR_MAX , you cannot know. – Jesper Juhl Nov 29 '18 at 18:02
  • 1
    I think your question is interesting, but the focus being on the size of char instead of reading from istream makes things less clear –  Nov 29 '18 at 18:02
  • See https://stackoverflow.com/questions/10335236/how-to-read-a-file-into-unsigned-char-array-from-stdifstream –  Nov 29 '18 at 18:05
  • @MaxLanghof What do you mean it is implementation-defined ? Could you please elaborate a little bit more ? – Marios Ath Nov 29 '18 at 18:09
  • 2
    @MariosAth meaning, the C++ standard does not define whether `char` by itself is signed or unsigned, only that it is a distinct type from `signed char` and `unsigned char` for purposes of function overloading and such. So compiler vendors have to decide for themselves in their compiler's implementation whether `char` is signed or unsigned – Remy Lebeau Nov 29 '18 at 18:12
  • @MariosAth Can you post some code to illustrate precisely where and why you need to know the range? The solution is probably just to adjust the code so that it isn't affected by the range of `char` so that your code stays portable. – David Schwartz Nov 29 '18 at 18:18

1 Answers1

1

If on your platform char is signed then its -128 to 127.

If on your platform char is unsigned then its 0 to 255

CHAR_MAX in limits.h will tell you which you have

This is of course all based on the assumption the you have 8 byte chars. CHAR_BIT will tell you that for sure

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 4
    I think you missed the words "at least" in each of the first two sentences. – Toby Speight Nov 29 '18 at 18:17
  • Your first sentence also assumes two's complement, which is not ([yet](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r0.html)) guaranteed. – Max Langhof Nov 30 '18 at 08:39