H Bellamy H Bellamy - 22 days ago 13
C++ Question

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?

Answer

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