Why is the behaviour of subtracting characters implementation specific?

前端 未结 3 1793
鱼传尺愫
鱼传尺愫 2021-02-01 13:42

This statement:

if(\'z\' - \'a\' == 25)

is not guaranteed to evaluate in the same way. It is compiler dependent. Also, it is not guaranteed to

3条回答
  •  难免孤独
    2021-02-01 14:18

    To expand on the other correct answers, a real-world example of a non-ASCII C compiler that’s still being used is IBM’s z/OS XL C/C++. By default, it assumes that source files are in IBM code page 1047 (the version of EBCDIC with the same repertoire as Latin-1). However, it has several different compiler options to support not only ASCII, but also “hybrid code,” or source files containing data in more than one encoding. (These programs exist because MVS compilers required syntax statements to be in IBM-1047 encoding only.)

    From the documentation, it looks like it would be possible to muck around with commands like #pragma CONVLIT(suspend) in a way that really would make those two statements evaluate differently on that compiler. I don’t have a copy to test a MCVE on.

提交回复
热议问题