What is the meaning of these strange question marks?

柔情痞子 提交于 2020-07-29 02:38:26

问题


I came across some weird-looking code. It doesn't even look like C, yet to my surprise it compiles and runs on my C compiler. Is this some non-standard extension to the C language and if so, what is the reason for it?

??=include <stdio.h>

int main()
??<
  const char arr[] = 
  ??<
    0xF0 ??! 0x0F,
    ??-0x00,
    0xAA ??' 0x55
  ??>;

  for(int i=0; i<sizeof(arr)/sizeof(*arr); i++)
  ??<
    printf("%X??/n", (unsigned char)arr??(i??));
  ??>

  return 0;
??>

Output:

FF
FF
FF

回答1:


The code is fully standard compliant to any version of the C standard. The ?? mechanism is called trigraphs and was introduced to C to allow an alternative way of printing certain symbols. It looks like the program was written as a demonstration of various trigraph sequences.

Back in the days, many computers and their keyboards were based on an old symbol table called ISO 646 which didn't contain all symbols used in the C language, such as \ { } [ ]. This made it impossible for programmers from some countries to even write C, because their national keyboard layout lacked the necessary symbols. Instead of remaking the keyboards and symbol tables, the C language was changed.

Therefore trigraphs were introduced. Today they are considered a completely obsolete feature and it is not recommended to use them. GCC will for example give you a warning if you use them. Still, they remain in the C standard for backwards-compatibility and all C compilers must support them.

The existing trigraph sequences are (C11 5.2.1.1 Trigraph sequences):

??=  #
??(  [
??/  \
??)  ]
??'  ^
??<  {
??!  |
??>  }
??-  ~

The left column is the trigraph sequence and the right column is its meaning.



来源:https://stackoverflow.com/questions/23825603/what-is-the-meaning-of-these-strange-question-marks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!