\? Control code

Hello,

As you know, control code '\?' was invented to prevent compiler using trigraphs in strings, e.g.:

cout << "?\?/n"; //"??/n" instead of "\n"

But why do we can put the backslash only before the second question mark ?

I mean, something like this won't print the whole trigraph:

cout << "\??/n"; //same as "\\n"

I'm just asking out of curiosity :)
I have not understood what are you asking about?!

In your last example


cout << "\??/n"; //same as "\\n"

the new line character will not be generatted. There will be two separate characters '\\' and 'n'.
Last edited on
Yes, every ??/ becomes \ at translation phase 1, before pretty much anything else

The escape sequences in string literals are processed at translation phase 5, which is when your \? becomes ?
I have not understood what are you asking about?!


Huh, I thought my question:

But why do we can put the backslash only before the second question mark ?


was quite obvious.

All I want to know is why:

"?\?/n"

not equivalent to:

"\??/n"

Is everything clear now?
It's because of the way the C pre-processor works: it converts trigraphs to their equivalent character before it converts escaped chars.

So for "?\?/n"

- replace trigraphs -> "?\?/n"
(the same, as no trigraph)

- replace escape sequences -> literal string "??/n"
("\?" is the same as "?")

but for "\??/n"

- replace trigraphs -> "\\n"
(as "??/" is the trigraph for "\")

- replace escape sequences -> literal string "\n"

that is, the \ between the ?s stops the trigraph from being spotted.

Andy

PS this is just repeating what has already been said, in a rather more verbose manner.
Last edited on
It's because of the way the C pre-processor works: it converts trigraphs to their equivalent character before it converts escaped chars.


Thx, I get it now.

PS. Escape character is a backslash, and escape sequence/control character is a backslash + proper character e.g. \n, right ?
Topic archived. No new replies allowed.