Print ?? and !! in different sequence will show different output
Asked Answered
T

5

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?

Tourist answered 5/10, 2010 at 10:5 Comment(3)
Explained below: To overcome just escape one of the question marks.Bellina
The best part of trigraphs is that if you don't know what they are called, it is impossible to search for them. :-) (Google isn't too helpful when asked about "??")Traumatize
@JamesMcNellis A search for '??!' at SymbolHound led me here :)Dustin
G
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.

Giagiacamo answered 6/10, 2010 at 5:28 Comment(0)
T
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.

Traumatize answered 5/10, 2010 at 10:7 Comment(3)
Or anywhere else, for that matter. :)Bautista
@jalf: I wish the ANSI C committee had followed your rule... ;-)Pika
I mainly just wish everyone who asked questions followed it ;)Bautista
P
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: |");
Pga answered 5/10, 2010 at 10:8 Comment(1)
All these examples are in strings, but it should be noted that trigraph replacement happens everywhere, not just in strings.Pika
G
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.

Giagiacamo answered 6/10, 2010 at 5:28 Comment(0)
S
4

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

Superstition answered 5/10, 2010 at 10:10 Comment(0)
S
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.

Selfrevealing answered 5/10, 2010 at 10:11 Comment(2)
... nowadays, instead, they are implemented just to make programming more interesting. :)Ionogen
@Matteo : True. Programming is def not interesting.Selfrevealing

© 2022 - 2024 — McMap. All rights reserved.