Why would the character 'A' be compared with 0x41?

I was looking at some C++ code and found the following construct:

if('A' == 0x41) {
  // ...
} else if('A' == 0xc1) {
  // ...
} else {
  // ...
}

I get a Visual Studio warning saying:

Warning C4127 conditional expression is constant.

Visual Studio is clearly right - surely 'A' is defined to be 0x41. Why is the author writing this code, given that two out of the three branches are dead code?


Solution 1:

0xc1 is the EBCDIC character set code for A. The author is testing for such a machine.

http://www.ibm.com/support/knowledgecenter/en/SSGH4D_15.1.3/com.ibm.xlf1513.aix.doc/language_ref/asciit.html

Solution 2:

At first sight might look like that is dead code but 'A' == 0x41 not always will return true..

what the developer tried to do here is lazily find what encoding is the machine implementing ASCII or any variant of EBCDIC

as @Richard suggested Capital a is mapped to 0xc1 in the International - Extended Binary Coded Decimal Interchange Code see table below in the 2 branch of the if else...

enter image description here

another different value could be found by ASCII for exmaple:

enter image description here

he could as well have done:

if('p' == 0x70) {
  // ...
} else if('p' == 0x97) {
  //...
}