Output - The Escape Sequence

In C++, cout is an instance of the ostream class in C++, and it is used to output data to the standard output, which is usually the console or terminal. The cout object is part of the iostream library, providing input and output operations functionality.

Escape sequences in C++ are used to represent special characters within string literals and character literals. An escape sequence starts with a backslash (\) followed by a character representing the special function.

Using the iostream

#include <iostream>

 

Escape Sequence

Description

\n

Newline

Cursor   moves to the beginning of the next line

\t

Tab

Cursor   moves to the next tab stop

\b

Backspace

Cursor   moves one space to the left

\r

Return

Cursor   moves to the beginning of the current line (not the next line)

\\

Backslash

Backslash   is printed

\'

Single   quotation

Single   quotation mark is printed

\"

Double   quotation

Double   quotation mark is printed

The insertion operator (<<) is used with cout to output data. Here's a simple example of how to use cout to print a message:

// Demo of using the "\" escape sequence #include <iostream> using namespace std; int main() { cout << "This is how to add a new\nline \n"; cout << "This is how to add a \tTAB \n"; cout << "This is how to print a backslash \\ \n"; cout << "This is how to print a single quote \' \n"; cout << "This is how to print a double quote \" \n"; cout << endl; //you can add a new line this way as well return 0; }

 

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark