Print ?? and !! in different sequence will show different output
I had found a strange output when I write the following lines in very simple way:
Code:
printf("LOL??!\n");
printf("LOL!!?\n");
Output:
It happens even the code is compiled under both MBCS and UNICODE.
The output varies on the开发者_JAVA百科 sequence of "?" and "!"...
Any idea?
??!
is a trigraph that gets replaced by |
.
As a rule, you should never place two question mark characters together anywhere in a source file.
You may try
printf( "What?\?!\n" );
In computer programming, digraphs and trigraphs are sequences of two and three characters respectively which are interpreted as one character by the programming language.
Some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by default and require an option to turn them on. Some can issue warnings when they encounter trigraphs in source files. Borland supplied a separate program, the trigraph preprocessor, to be used only when trigraph processing is desired.
They are called Trigraph Sequences
??!
is the trigraph sequence for Vertical Bar |
.
The C/C++ preprocessor recognizes the trigraphs and replaces them with their equivalent character.
So by the time your code is seen by the compiler, the trigraphs are already replaced.
# grepping in the source file:
$ grep printf a.c
printf("foo: ??!");
# grepping the preprocessor output:
$ gcc a.c -trigraphs -E | grep printf | grep foo
printf("foo: |");
The ??! is known as trigraph and is replaced with | in output. Check this link
It is a special sequence of characters in a string constant that has a special meaning. Called a trigraph they were originally implemented because not all terminals supported some characters.
精彩评论