char and int

I have a basic questions about char and int...what happens when I assign an int to a char like thischar ch = 1; do i get the character 1 or the equivalent to ASCII code 1...and when I do this char ch = 'A', do i have the character A? and when char ch= 3 + 'A' do I get D? I have an idea of everything but I always get confused....someone could give a general explanation to deduce by myself each time this happens??

closed account (48T7M4Gy)
char c = 1 assigns ascii code 1 to c
char c = '1' assigns character ascii code 49 to c
when char ch= 3 + 'A' do I get D?
If your target machine uses ASCII, or compatible, as its encoding then yes. But generally it is not a good idea to rely on it.
Last edited on
then when I do this char += '1' what is in char is considered like the ascii code???
closed account (48T7M4Gy)
The best idea is to try it out and google an ASCII chart.

char += '1' will add 49 to whatever the earlier value was so char '1' += '1' will be ascii 98. Keep in mind to their is a limit/set range depending on whether its an extended table or not

1
2
char c = '1';
std::cout << c + '1';


should display 98
Last edited on
closed account (48T7M4Gy)
1
2
char c = 'A';
  std::cout << (char)(3 + c);


D
Last edited on
ok, cool!!
Topic archived. No new replies allowed.