Printing text in C++

Aarav Iyer
3 min readMay 11, 2023

--

C++ Output

There is a simple way of printing text in C++. You can use the cout object along with the insertion operator, <<.

You need to write all the necessary lines of C++ code first and inside a curly bracket {}, write cout<< and then write the text in double quotes “”. Also remember to add the semi-colon at the end of the line.

This gives whatever is present in the “” as output.

For example-

#include <iostream>

using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}

The output-

Hello World!
File pic of C++ code.

Escape Sequences

Moving The Cursor To The Next Line (\n)

You can insert a new line in your output by adding the \n newline character. For example-

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \n";
cout << "C++ is fun!";
return 0;
}

The output-

Hello World!
C++ is fun!

The number of new lines created will be equal to the number of \n characters written.

Using endl

endl is a manipulator that can also be used to insert a new line in C++. It’s functionality is the same as \n but it does not occupy any memory (\n uses 1 byte).

On the other end, an advantage of \n is that it is also valid in C, making life easier for people programming using multiple languages.

Here’s an example using endl-

# include <iostream>

using namespace std;
int main()
{
cout << "Hello World!" << endl;
cout << "C++ is fun!";
return 0;
}

The output-

Hello World!
C++ is fun!

Horizontal Tabs

You can add a horizontal tab by writing the \t escape sequence at the end of the line just like \n.

For example-

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \t";
cout << "C++ is fun!";
return 0;
}

The output-

Hello World!  C++ is fun

Other Common Escape Sequences

If you want to add a character (in a string) that has some use as per the C++ syntax, you can add a backslash \ before the character.

For example-

#include <iostream>
using namespace std;
int main()
{
cout << "\"C++\" is fun!" << endl;
cout << "Here's a backslash. \\";
return 0;
}

The output-

"C++" is fun!

This code uses \ to print double quotes " and a backslash \. Without the escape sequence, the " or \ would have been interpreted incorrectly by the compiler.

The printf() function

The printf() function can also be used instead of cout. This method has various advantages over cout, like being faster and giving you more control over formatting.

printf is a part of the C standard library, and is available across different languages like C, C++, Python, etc, making it easier for you while working in a multi-language environment.

Here’s an example using printf-

#include <iostream>
using namespace std;
int main()
{
printf("Hello World! C++ is fun!");
return 0;
}

The output-

Hello World! C++ is fun!

That is all that you need to know (for a beginner coder) about printing text in C++. Follow me for more posts on coding!

Please consider applauding me if this story was easy to understand and informative. Feel free to inform me of anything I might have missed or can improve by dropping a response.

Check out Code Streak, my blog exclusively devoted to coding.

Regards,
Aarav Iyer

--

--

Aarav Iyer
Aarav Iyer

Written by Aarav Iyer

A tech enthusiast learning Python and interested in anything related to space and the Universe.

No responses yet