This online C++ compiler runs simple C++ programs in your browser. A Hello World program is in the editor already. Press Run to see the output, then change the code and run it again. It suits the first weeks of learning C++, when your programs use cout, cin, loops, arrays and functions.
It is an interpreter for a basic subset of C++, not the g++ compiler. Classes, std::string and vector do not work here, and the page says so plainly below, so you know when to switch to a real compiler.
How to run C++ online
- Write your program in the C++ code editor, or click an Example such as Loop or Function.
- If the program uses
cin, type the input in the Input (stdin) box, for example5 7. - Press Run or Ctrl + Enter.
- Read the Output. The exit code, or an error with its line number, appears under it.
- Press Stop to end a program early. Programs still running after 5 seconds are stopped automatically.
Hello World in C++
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Output:
Hello, World!
Keep the line using namespace std;. This interpreter does not accept the std::cout form, so std::cout << "Hi"; gives an error here even though g++ accepts it.
Run C++ programs with cin input
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum = " << a + b << endl;
return 0;
}
With 5 7 in the Input box, the output is:
Enter two numbers: Sum = 12
The numbers you type do not appear after the prompt, because they come from the box and not from a keyboard. while (cin >> x) loops also work: with 4 8 15 16 23 42 as input, a loop that counts and adds the numbers prints 6 numbers, sum 108.
Common mistake: A common mistake is running a
cinprogram with an empty Input box.cinhas nothing to read, so the variable keeps no real value and the output can showNaNor an error. Type one value for every>>before you press Run.
Decimal output, the C++ way
cout prints a double with up to 6 significant digits, the same as g++:
#include <iostream>
using namespace std;
int main() {
int marks[5] = {78, 85, 92, 64, 88};
int total = 0;
for (int i = 0; i < 5; i++) {
total += marks[i];
}
cout << "Average = " << total / 5.0 << endl;
cout << "One third = " << 1.0 / 3 << endl;
return 0;
}
Output:
Average = 81.4
One third = 0.333333
For a fixed number of decimal places, include <iomanip> and write cout << fixed << setprecision(2);. setw and setfill work too.
What this C++ online editor supports
We ran beginner programs through it to find out. This is what we found.
| Works | Does not work |
|---|---|
cout, cin, endl, cin.getline into a char array |
class, struct, objects, constructors, destructors |
if, else, switch, for, while, do while |
std::string, vector and other STL containers |
| Arrays, 2D arrays and char arrays as strings | #include <string>, <vector>, <algorithm>, <bits/stdc++.h> |
| Functions, recursion and default arguments | Function overloading and templates |
Pointers, int *p = &x, swap(&a, &b) |
References such as void f(int &x) |
<iomanip>: fixed, setprecision, setw, setfill |
new and delete, try and catch |
<cmath>: sqrt, pow, abs; bool, const, inline |
auto, range-based for (int x : arr), the std:: prefix |
C code with printf and scanf |
round, boolalpha, namespaces other than std |
The biggest gap is object-oriented programming. If you are working through C++ classes and objects or constructors and destructors, those programs need g++. Here is what you see if you try a class:
#include <iostream>
using namespace std;
class Student {
public:
int marks;
};
int main() {
Student s;
return 0;
}
Not supported here on line 4: Syntax error near "{".
4 | class Student {
^
This in-browser interpreter does not support classes. It runs a beginner subset of C++. To run this program, install g++ on your computer.
Other differences from g++: an int that overflows stops the program with an error instead of wrapping around, and the interpreter is slow, so a loop of about 100,000 steps takes a few seconds and very large loops hit the 5-second limit.
When to install g++
Install a compiler when you reach classes, strings or the STL, or when you want results that match your lab exactly. The steps for gcc in install a C compiler and run your first program also install g++, the C++ compiler. After that, compile with g++ program.cpp -o program and run ./program.
Learn C++ step by step
- Start with the C++ tutorial for beginners and run its early examples here.
- Coming from C? Most C programs for beginners also run in this tool, and the online C compiler is set up for C.
- Preparing for placements? Read the C++ interview questions for freshers and try the short code answers here.
Pro tip: Add a
coutline inside a loop to print the loop variable and your running total on each pass. When the final answer is wrong, the printed values show you the exact step where it went off track.
