C++ Output (Print Text)
In this article or you can say blog, we shall discuss C++ Output of our C++ code or how we can Print text okay. So for that purpose, we use cout object along with << operator is used to print values or output text okay.
For example:
Let’s look over this code:
#include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; }
Output:
Hello World!
Now You can add as many cout objects as you want. However, note that it does not insert a new line at the end of the output:
For example:
#include <iostream> using namespace std; int main() { cout << "Hello World!"; cout << "I am learning C++ wih Codelikechamp.com"; return 0; }
Output:
Hello World!I am learning C++ wih Codelikechamp.com
Now the question is how can we add a line to our code so it will also add a line in our output for that purpose, we take the help of Escape sequences; see below how you can do that.
New Line in C++ Output
To insert a new line, you can use the \n character: which is Escape Sequences
For example:
#include <iostream> using namespace std; int main() { cout << "Hello World! \n"; cout << "I am learning C++"; return 0; }
Output:
Hello World! I am learning C++
NOTE : Two \n characters after each other will create a blank line:
For example:
#include <iostream> using namespace std; int main() { cout << "Hello World! \n\n"; cout << "I am learning C++"; return 0; }
Output:
Hello World! I am learning C++
NOTE: You can try more escape sequences for you own purpose or need here is the list you can go through it.
Escape Sequences List
Escape sequence | Character represented |
\a | Alert (Beep, Bell) (added in C89) |
\b | Backspace |
\e | escape character |
\f | Form feed Page Break |
\n | Newline (Line Feed) |
\r | Carriage Return |
\t | Horizontal Tab |
\v | Vertical Tab |
\\ | Backslash |
\’ | Apostrophe or single quotation mark |
\” | Double quotation mark |
\? | Question mark (used to avoid trigraphs) |
Link: https://Codelikechamp.com
Medium Link: Follow me on Medium
Linkedin Link: Follow me on Linkedin