2

I know that you can store integers up to like 100 and something in a char data type, but what about storing char data as in 'a' or 'b' in an int? I tried it and it seemed to work, but I'm not too sure if it's safe. Is it? Can I create an array of ints and use this array to store data in the form of 'x', 'b'...etc?

user121615
  • 199
  • 1
  • 7
  • You could certainly if you just want to store the characters, but if you know they will certainly be char, will this not be a overkill? – Shamim Hafiz - MSFT Apr 19 '15 at 03:41
  • 1
    yes, the character `'a'` has an integral representation. `int i = 'a';` is valid. – Ryan Haining Apr 19 '15 at 03:43
  • yeah, but I also want to store integers in the array as well. So It makes it easier for me. – user121615 Apr 19 '15 at 03:54
  • the biggest point you have to take from here is that `char` is an integral type. So yes you can store characters in `int` and integrals in `char`, provided the value is representable in range. The main difference between char and int is that they are treated differently when displayed: a `char` will display the ascii letter corresponding to the code (a number), an `int` will display the number itself – bolov Apr 20 '15 at 14:22

2 Answers2

1

Yes; char is guaranteed to be smaller than int.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    For the pedantic: guaranteed to be no larger than `int`, anyway. In theory, the two could (perhaps) be the same size (though parts of the library implicitly assume that `int` is larger). – Jerry Coffin Apr 19 '15 at 03:57
1

Characters such as 'a' and 'b' simply represent integer values in the char data type. int is at least as wide as char, that is, it represents a superset of its values. So yes, perfectly safe.

Usually you will have sizeof(int) > sizeof(char), because the standard library wants to reserve one value for EOF (end-of-file). Technically, this luxury is optional, or at least unreliable, and you should use eofbit and such for compatibility with esoteric systems.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 1
    "int must represent more values than char" — this is not true, it's perfectly OK if int and char have the same width. int must be able to store all *members of the execution character set* (not the same as "all values of type char") plus EOF. – n. m. could be an AI Apr 19 '15 at 05:39
  • @n.m. I don't think so. My recollection, and the simplest interpretation of C11 §7.21.2 (given §3.7.1) is that a C binary-mode file is a stream of `char` values, and all such values must be preserved, not only the execution character set. The end-of-file value must be outside the range of `char`. – Potatoswatter Apr 19 '15 at 15:20
  • http://stackoverflow.com/questions/3860943/can-sizeofint-ever-be-1-on-a-hosted-implementation – n. m. could be an AI Apr 20 '15 at 08:33
  • Yes, that's the right word. Perverse. I think an old Cray C compiler used to have `sizeof(int)==1` but now I'm not sure that it was standards conforming, – n. m. could be an AI Apr 20 '15 at 14:18