Question about Escape sequences

Part I
======
1
2
std::cout << "Backslash: \\" << std::endl;
std::cout << "Double quotes: \"" << std::endl;


I can understand the need of a backslash to input the double quote and the backslash itself, we cannot do otherwise without it.

But what about the single quote and the question mark?
1
2
std::cout << "Single quote: '" << std::endl;
std::cout << "question mark: ?" << std::endl;


In code above I can output them without the backslash, so why we need that backslash?

===========================================

Part II
=======
std::cout << "\tCareful a tab!\tI told you" << std::endl;
Here the first tabulation is 8 white characters, but the second one is only 2 characters. Why is that?

===========================================

Part III
========
I understand and I can use \a, \n and \r, but what about \v and \f, does anyone knows a visual use of them, that is, for \v, I expect 8 white lines, but I do not get it, instead I got a weird character(a box with a question mark inside it).

Escaping the single quote is necessary when writing a character literal.
 
std::cout << '\'';


Before C++17 there were something called "trigraphs" which used two question marks in combination with another character as an alternative way of writing certain character. For example, instead of writing { you could write ??< and instead of # you could write ??=.

So if you wanted to print "??=" before C++17 you would have to escape the question marks (at least one of them).

1
2
std::cout << "??="; // printed "#" before C++17
std::cout << "\?\?="; // prints "??=" 

https://en.cppreference.com/w/cpp/language/operator_alternative#Trigraphs_.28removed_in_C.2B.2B17.29
Last edited on
I have no experience with using \v and \f. It's not really up to C++ how to handle them. Instead it depends on whatever receives the output. I guess these characters might have been more commonly used in the old days with terminal computers and primitive printing devices.

That said, for me on Linux this code
 
std::cout << "one\vtwo\fthree";
gives the following output

one
   two
      three
Last edited on
std::cout << "\tCareful a tab!\tI told you" << std::endl;
Here the first tabulation is 8 white characters, but the second one is only 2 characters. Why is that?

If your tab width is 8 then you can think of each row as being split up into groups of 8 characters. A tab will take you to the start of the next group.

|       |       |       |       |      
1234567812345678123456781234567812345678
        Careful a tab!  I told you
Last edited on
Escaping the single quote is necessary when writing a character literal.
std::cout << '\'';


Genius, despite it is simple, could not think of it.

If your tab width is 8 then you can think of each row as being split up into groups of 8 characters. A tab will take you to the start of the next group....


Got it)))))))
I will ask about trigraphs later.

Thank you very much Mr @Peter)))
That said, for me on Linux this code
std::cout << "one\vtwo\fthree";
gives the following output


Not with Windows. It just shows symbols.

\v and \f were mainly used with printers where \f moved to the top of the next page of output and \v was as set by the printer setup. These are the ASCII codes 0x0c (/f) and 0x0b (/v). /v stands for vertical tab and /f is for formfeed.

https://en.cppreference.com/w/cpp/language/escape

Thank you @seeplus
Topic archived. No new replies allowed.