5

I had found a strange output when I write the following lines in very simple way:

Code:

 printf("LOL??!\n");
 printf("LOL!!?\n");

Output: alt text

It happens even the code is compiled under both MBCS and UNICODE.

The output varies on the sequence of "?" and "!"...

Any idea?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
wengseng
  • 1,330
  • 1
  • 14
  • 27

5 Answers5

16

??! is a trigraph that gets replaced by |.

As a rule, you should never place two question mark characters together anywhere in a source file.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
5

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: |");
codaddict
  • 445,704
  • 82
  • 492
  • 529
5

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.

stackfull
  • 66
  • 2
4

The ??! is known as trigraph and is replaced with | in output. Check this link

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
2

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.

Hogan
  • 69,564
  • 10
  • 76
  • 117