| Quentin (47) | |
|
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 :) | |
|
|
|
| vlad from moscow (3662) | |
|
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
|
|
| Cubbi (1927) | |
Yes, every ??/ becomes \ at translation phase 1, before pretty much anything elseThe escape sequences in string literals are processed at translation phase 5, which is when your \? becomes ? | |
|
|
|
| Quentin (47) | |||
Huh, I thought my question:
was quite obvious. All I want to know is why: "?\?/n"not equivalent to: "\??/n"Is everything clear now? | |||
|
|
|||
| andywestken (1966) | |
|
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
|
|
| Quentin (47) | ||
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 ? | ||
|
|
||