C++ Get Started
Getting started is the path from a written code file to a running program. A compiler turns the file into a program first, and then the computer runs it.
The full shape
This program shows every part of the basic shape. Each part has one job.
#include <iostream>
int main() {
std::cout << "Let's get started.\n";
return 0;
}Two statements in order
Two print statements sit inside main. Each statement prints one line.
#include <iostream>
int main() {
std::cout << "First step\n";
std::cout << "Second step\n";
return 0;
}Example
#include <iostream>
int main() {
std::cout << "Program started.\n";
std::cout << "Program finished.\n";
return 0;
}Key points
- A C++ program in this course lives in a file named main.cpp.
- Use g++ -std=c++17 main.cpp -o main to compile the program.
- Use ./main to run the compiled program.
- The #include <iostream> line brings in tools for printing text.
- int main() is the place where running starts.
- A statement is one complete instruction in a program.
- Each statement runs in order from top to bottom.
- The return 0; line reports success at the end of the program.
In this example, the first statement prints Program started. The second statement prints Program finished. The lines appear in the order of the code.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz C++ Compiler
Exercise
Which command compiles the program in main.cpp?