views:

211

answers:

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?

+15  A: 

??! 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
http://en.wikipedia.org/wiki/Digraphs_and_trigraphs
ereOn
Or anywhere else, for that matter. :)
jalf
@jalf: I wish the ANSI C committee had followed your rule... ;-)
R..
I mainly just wish everyone who asked questions followed it ;)
jalf
+5  A: 

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
All these examples are in strings, but it should be noted that trigraph replacement happens **everywhere**, not just in strings.
R..
+4  A: 

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

Sachin Shanbhag
+2  A: 

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
... nowadays, instead, they are implemented just to make programming more interesting. :)
Matteo Italia
@DevSolar : ??? What does your comment mean?
Hogan
@Matteo : True. Programming is def not interesting.
Hogan
+4  A: 

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