cpp / beginner
Snippet
Printing Output with cout
The cout object sends data to the standard output (usually the screen). The << operator, called the stream insertion operator, chains multiple values together. endl adds a newline and flushes the output buffer.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>using namespace std;int main() {cout << "Hello, World!" << endl;cout << "Number: " << 42 << endl;cout << "Sum: " << 10 + 32 << endl;int a = 5, b = 8;cout << a << " + " << b << " = " << a + b << endl;return 0;}
Breakdown
1
cout << "Hello, World!" << endl;
Outputs the string and moves to a new line
2
cout << "Number: " << 42 << endl;
Combines string literal with a number in one output statement
3
cout << "Sum: " << 10 + 32 << endl;
Can output the result of expressions directly
4
int a = 5, b = 8;
Declares two integers in a single statement
5
cout << a << " + " << b << " = " << a + b << endl;
Chains multiple values including variables and computed results