C++ Output (cout <<) C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ Output (cout <<)
C++ Output: cout <<
The cout
object, together with the <<
operator is used to output values/print text. You can add as many cout
objects as you want.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "My C++ Program";
return 0;
}
Output:
Hello World!My C++ Program
New Lines
To insert a new line, you can use the \n
character.
Note: 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 << "My C++ Program";
return 0;
}
Output:
Hello World!
My C++ Program
endl
This is another way to insert a new line. With the endl
manipulator, you can use a newline.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "My C++ Program";
return 0;
}
Output:
Hello World!
My C++ Program
Both \n
and endl
are used to break lines. However, \n
is used more often and is the preferred way.