Why char a = 65 is less portable than char a = 'A'

I just asked this question . What is the reason for the double negation - (- n)? where the defendant mentions in the comment that

char a = 'A';

more portable than

char a = 65;

Why is this? Are they exactly the same?

+5
source share
3 answers

A is represented by 65 in ASCII; but this does not mean that each symbol system will represent it at 65.

If you write

 char a = 65; 

then you mean that the system will use ASCII. It is less portable than writing

 char a = 'A'; 

which does not make this assumption.

+5
source

65 is ASCII code for A that can work on many systems, but what if the system does not support ASCII or does not have 65 for A ? If this happens, then it is less portable due to system dependency.

 char a = 'A'; 

is a char literal that will work on all systems, but:

 char a = 65; 

is ASCII and may not work on all systems. Basically, you depend on the fact that the user uses ASCII, and depending on the user is never reliable.

+2
source

If the variable a should store the letter "A", then it is better to write

 char a = 'A'; 

because it is more portable, because there may be other character encodings than ASCII, but also because the reader reads that a stores the letter "A".

The opposite case is even more obvious: no one will ever write

 int a = 'A'; 

when a must contain the value "65" for the calculation.

+1
source

All Articles