0% found this document useful (0 votes)
30 views2 pages

C++ Output (Print Text) : Example

The document explains how to use the cout object and the << operator in C++ to output text. It describes how to print multiple lines and insert new lines using the character or the endl manipulator. It notes that while both methods can be used to break lines, is more commonly preferred.

Uploaded by

geloserty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

C++ Output (Print Text) : Example

The document explains how to use the cout object and the << operator in C++ to output text. It describes how to print multiple lines and insert new lines using the character or the endl manipulator. It notes that while both methods can be used to break lines, is more commonly preferred.

Uploaded by

geloserty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Output (Print Text)

The cout object, together with the << operator, is used to output values/print text:

Example
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}
Try it Yourself »

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:

Example
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  cout << "I am learning C++";
  return 0;
}
Try it Yourself »

New Lines
To insert a new line, you can use the \n character:

Example
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World! \n";
  cout << "I am learning C++";
  return 0;
}
Try it Yourself »

Tip: Two \n characters after each other will create a blank line:


Example
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World! \n\n";
  cout << "I am learning C++";
  return 0;
}
Try it Yourself »

Another way to insert a new line, is with the endl manipulator:

Example
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!" << endl;
  cout << "I am learning C++";
  return 0;
}
Try it Yourself »
Both \n and endl are used to break lines. However, \n is used more often and is the
preferred way.

You might also like