C++ · Course navigation

C++ Basics

Write and run a first program.

On this page

The concept

A C++ program begins in main. Include iostream to write to standard output with cout. The line using namespace std; lets you write cout instead of std::cout.

Code example

C++
#include <iostream>
using namespace std;

int main() {
  cout << "Hello Programonic\n";
}
Output
Hello Programonic

With the std:: prefix

The standard library lives in the namespace std. The program above uses using namespace std; so that names such as cout can be written without a prefix, which is the simplest way to start. You can also leave the using line out and write std::cout instead. Both programs do exactly the same thing. The std:: prefix is the safer habit in larger programs and header files, because it avoids name clashes.

C++
#include <iostream>

int main() {
  std::cout << "Hello Programonic\n";
}
Output
Hello Programonic

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1#include <iostream>
2using namespace std;
3
4int main() {
5 cout << "Hello Programonic\n";
6}

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
Program execution enters main.—
—
Write the string and newline to standard output.—
Hello Programonic

Behind the scenes

How C++ processes this code

The C++ compiler produces a native executable. Execution enters main, where objects and function calls perform the work.