C++ · Playground
C++ playground
Running C++ inside the browser is not available yet. Use this editor to write, then download the file and run it locally with GCC 13 (C++17). The example snippets were verified that way.
#include <iostream>
int main() {
std::cout << "Welcome to the Riverside Library\n";
std::cout << "Opening hours: 9 to 17" << std::endl;
std::cout << "Books on loan: " << 42 << '\n';
return 0;
}Output
Press Run to see the program’s output here.
Running C++ inside the browser is not available yet. Use this editor to write, then download the file and run it locally with GCC 13 (C++17). The example snippets were verified that way.
C++ examples
Each one was run with GCC 13 (C++17) before it was published. Load one, change it, run it.
- C++ · C++ Syntax and Your First Program: Printing three lines
#include <iostream> int main() { std::cout << "Welcome to the Riverside Library\n"; std::cout << "Opening hours: 9 to 17" << std::endl; std::cout << "Books on loan: " << 42 << '\n'; return 0; } - C++ · C++ Syntax and Your First Program: Statements, blocks and comments
#include <iostream> // A program is a list of statements; each one ends with a semicolon. int main() { int shelves = 4; // a declaration is a statement too int perShelf = 30; std::cout << "Capacity: " << shelves * perShelf // one statement spread over three lines << " books\n"; /* Braces group statements into a block. A variable declared inside a block stops existing at its closing brace. */ { int extra = 12; std::cout << "With the trolley: " << shelves * perShelf + extra << "\n"; } return 0; } - C++ · Variables and Types in C++: Declaring one of each
#include <iostream> #include <string> int main() { int loaves = 23; // a whole number double pricePerLoaf = 2.75; // a number with a fractional part char grade = 'A'; // one character, in single quotes bool sourdough = true; // true or false std::string bakery = "Mill Lane Bakery"; long long yearlyCustomers = 12000000000LL; std::cout << bakery << "\n"; std::cout << "Loaves: " << loaves << "\n"; std::cout << "Stock value: " << loaves * pricePerLoaf << "\n"; std::cout << "Grade: " << grade << "\n"; std::cout << "Sourdough: " << sourdough << "\n"; std::cout << std::boolalpha; std::cout << "Sourdough: " << sourdough << "\n"; std::cout << "Customers: " << yearlyCustomers << "\n"; return 0; } - C++ · Variables and Types in C++: const, auto and characters as numbers
#include <iostream> int main() { const int maxSeats = 48; int booked{31}; // brace initialisation auto remaining = maxSeats - booked; // deduced as int auto fillRate = booked / static_cast<double>(maxSeats); // deduced as double std::cout << "Remaining: " << remaining << "\n"; std::cout << "Fill rate: " << fillRate << "\n"; // maxSeats = 50; would not compile: maxSeats is const char letter = 'b'; letter = letter + 1; // a char is a small integer underneath std::cout << "Next letter: " << letter << "\n"; std::cout << "Code of 'A': " << static_cast<int>('A') << "\n"; return 0; } - C++ · Operators in C++: Integer division, remainder and precedence
#include <iostream> int main() { int eggs = 75; int perBox = 12; std::cout << "Full boxes: " << eggs / perBox << "\n"; std::cout << "Left over: " << eggs % perBox << "\n"; std::cout << "Boxes as a decimal: " << eggs / 12.0 << "\n"; std::cout << "7 / 2 = " << 7 / 2 << "\n"; std::cout << "-7 / 2 = " << -7 / 2 << "\n"; std::cout << "-7 % 2 = " << -7 % 2 << "\n"; std::cout << "2 + 3 * 4 = " << 2 + 3 * 4 << "\n"; std::cout << "(2 + 3) * 4 = " << (2 + 3) * 4 << "\n"; return 0; }
How this page was checked. Every program on it was run with GCC 13 (C++17) at build time by the publishing checks, and the output shown is what it printed. Running C++ inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with GCC 13 (C++17) locally.