Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

What exactly happens when I try to assign a non ASCII value to a char variable?

I know that the way chars are stored is that they're first converted to their integer equivalents from the ASCII table and that integer is converted to binary. I'm also aware of the fact that if I assign an integer value to a char which is greater than any of the integer equivalents in the ASCII table then only the last 8 bits of the binary representation of that integer will be stored in my char. But what I don't understand is what's going on when I insert a "char" value which is not a valid character in a char variable.

Kindly also correct me if my understanding of how they're stored is wrong.


Here's the code.

#include <iostream>
using namespace std;

int main() {
  char ch = '©';
  char ch1 = '10';
  cout << ch << endl;
  cout << ch1 << endl;
}


and here's the output in VS Code.

hi.cpp:5:13: warning: multi-character character constant [-Wmultichar]

  5 |  char ch = '┬⌐';

   |       ^~~

hi.cpp:6:14: warning: multi-character character constant [-Wmultichar]

  6 |  char ch1 = '10';

   |       ^~~~

hi.cpp: In function 'int main()':

hi.cpp:5:13: warning: overflow in conversion from 'int' to 'char' changes value from '49833' to ''\37777777651'' [-Woverflow]

  5 |  char ch = '┬⌐';

   |       ^~~

hi.cpp:6:14: warning: overflow in conversion from 'int' to 'char' changes value from '12592' to ''0'' [-Woverflow]

  6 |  char ch1 = '10';

   |       ^~~~

0

Sign In or Register to comment.