cpp / beginner
Snippet
Printing Output with cout
The cout object from the iostream library sends data to the console. The << operator (called stream insertion) chains multiple values together. endln inserts a newline character and flushes the output buffer, ensuring the text appears immediately.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>#include <string>int main() {std::cout << "Hello, World!" << std::endl;std::cout << "Welcome to C++" << std::endl;int age = 25;std::cout << "I am " << age << " years old." << std::endl;std::string name = "Max";std::cout << "My name is " << name << std::endl;return 0;}
Breakdown
1
#include <iostream>
Includes the input/output stream library for cout and cin
2
#include <string>
Includes the string class for text manipulation
3
int main() {
Main function where program execution begins
4
std::cout << "Hello, World!" << std::endl;
Prints text to console using stream insertion operator
5
int age = 25;
Declares an integer variable to store age
6
std::cout << "I am " << age << " years old." << std::endl;
Chains strings and variables for mixed output
7
std::string name = "Max";
Creates a string variable to hold text data