
You can chain << as many times as you like. Numbers go without quotes — if you put quotes around a number it becomes text, not a number.
ON THIS PAGE
SHOW EXAMPLE IN
//THE EXAMPLE
main.cpp
#include <iostream>
using namespace std;
int main() {
cout << "My name is Bat" << endl;
cout << "I am " << 14 << " years old" << endl;
cout << 2 + 3 << endl;
return 0;
}> what you see
My name is Bat I am 14 years old 5
//LINE BY LINE
Watch it run
The first statement sends one piece. Quotes only mark where the text ends — they are not printed.
Printing several things at once
You can chain << to mix text, numbers and variables in one line. Each << means "and then send this too".
int age = 14;
cout << "I am " << age << " years old" << endl;> what you see
I am 14 years old
Anything inside quotes is printed exactly. cout << "age"; prints the word age, not 14.
endl and \n
Both move to a new line. \n is just a character; endl also flushes the output buffer, which makes it a little slower.
cout << "line one" << endl;
cout << "line two\n"; // ижил үр дүн
cout << "a\nb\nc\n"; // гурван мөр> what you see
line one line two a b c
If you print a lot, \n is faster. For ordinary exercises the difference does not matter.
Escape characters
Some characters cannot be typed directly inside a string — a quote, for example. You escape them with a backslash.
| You write | You get |
|---|---|
| \n | шинэ мөр / new line |
| \t | таб зай / a tab |
| \" | хашилт " / a quote |
| \\ | ташуу зураас \ / a backslash |
cout << "She said \"hi\"\n";
cout << "a\tb\tc\n";> what you see
She said "hi" a b c