Reputation: 22715
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?
Upvotes: 90
Views: 9089
Reputation: 69902
0xc1
is the EBCDIC
character set code for A
. The author is testing for such a machine.
Upvotes: 118
Reputation: 48288
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...
another different value could be found by ASCII for exmaple:
he could as well have done:
if('p' == 0x70) {
// ...
} else if('p' == 0x97) {
//...
}
Upvotes: 11