Loading lessons...
Printing Text with cout
Printing Text with cout
The easiest way to get words on the screen: cout plus << plus text in quotes.
One line
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!";
return 0;
}
This prints: Hello, world!
Two cout statements, same line
Both cout statements write with no break in between, so the console shows one line:
cout << "Hello, world!";
cout << " I am learning C++";
Output:
Hello, world! I am learning C++
Chaining many pieces
You can glue several parts together in one statement:
cout << "Name is " << "Bilbo" << ".";
Result: Name is Bilbo.
The golden rules
- Put text inside double quotes
"...". - Each
<<pushes the next piece out. - Without
endlor\n, everything stays on the same line.
TL;DR
cout << "text"prints text.- Two
coutlines without a newline share one console line. - Chain pieces:
cout << "a" << "b" << "c"; - Add
\norendlto jump to a new line (that's the next lesson!).