Codectionary / Developer documentation / C++

Your First C++ Program

Write a complete Hello World program, understand every important part, compile it with C++17 and correct a first compiler error.

Start with one installed C++ toolchain

This walkthrough creates a small console program: it writes one greeting to the terminal and exits. You need a plain-text editor, a terminal and a C++ compiler. Install only the toolchain for your operating system before following its build commands below. Visual Studio with MSVC is the recommended beginner path on Windows, Apple Clang Command Line Tools on macOS, and your distribution’s g++ package on Linux.

Use the same folder for your source file and terminal commands. You can write the file in any editor, including Visual Studio, but saving text as main.cpp is essential: main.cpp.txt is a different filename. The commands in this page use C++17 explicitly so that the example does not rely on a compiler’s default language mode.

Create main.cpp

Create a folder you can find again, open it in your editor and save the complete program below as main.cpp. It is a complete file, not code to paste inside another function. Keep the quotation marks, braces and semicolons as shown. The expected terminal output is Hello, Codectionary! followed by a new line.

Source code is readable text, not yet an executable program. First the compiler checks and translates it; then the toolchain links the needed library code. Only after a successful build should you run the new executable. Saving an edit without rebuilding leaves the old executable unchanged.

main.cpp

#include <iostream>

int main() {
    std::cout << "Hello, Codectionary!\n";
    return 0;
}

What the header and standard library provide

#include <iostream> is a preprocessing directive. It makes the declarations in the standard input/output stream header available to this source file, including the declaration of std::cout. The angle brackets tell the toolchain to search its configured header locations. This directive is not a statement inside main and has no trailing semicolon.

The C++ standard library is the specified collection of facilities that a conforming implementation supplies, including streams, strings and containers. The compiler toolchain provides an implementation of those facilities. Including <iostream> gives the compiler the declarations needed to understand std::cout; the build also links the required library implementation. For this program you do not need to install a separate iostream package.

Where execution begins

int main() defines the program’s entry function in an ordinary hosted C++ application. The empty parentheses mean this version takes no parameters. The braces { and } enclose the function body: the statements that run when the program starts. Indentation makes the body easier to read but the braces determine its scope.

The output expression ends with a semicolon because it is an expression statement. return 0; also ends with a semicolon: it returns the integer zero to the host environment, conventionally indicating successful completion. If control reaches the closing brace of main without an explicit return, standard C++ acts as if return 0; were written there. Keeping it here helps a beginner see the exit status. Do not infer from this special rule that every non-void function can omit its return statement.

Linux: build with G++

Open a terminal in the folder containing main.cpp. Run the first command to compile and link the source into an executable called hello. Run the second command only if the build succeeds; ./ means the current directory. The output should be Hello, Codectionary!. This path uses the g++ package installed in the Linux guide.

In g++ -std=c++17 main.cpp -o hello, g++ is the C++ compiler driver; -std=c++17 selects the C++17 language mode; main.cpp is the input source; and -o hello names the executable. The driver also arranges linking of the C++ standard library. You can add -Wall -Wextra later for useful warnings. Changing the source requires another build.

Linux terminal, from the main.cpp folder

g++ -std=c++17 main.cpp -o hello
./hello

macOS: build with Apple Clang

Open Terminal in the folder containing main.cpp. Apple Command Line Tools provides clang++, a C++ compiler driver. Its -std=c++17 and -o options have the same purpose here as in the G++ command. Build first, then run the created file with ./hello. You do not need the full Xcode app for this terminal exercise.

If clang++ reports that developer tools are missing, complete the Command Line Tools installation described in the macOS guide before retrying. If the build fails, do not interpret an older hello executable’s output as evidence that the new source compiled.

macOS Terminal, from the main.cpp folder

clang++ -std=c++17 main.cpp -o hello
./hello

Windows: build with MSVC

If you followed the Windows guide, open the Developer Command Prompt for Visual Studio, change to the folder containing main.cpp and run the commands below. cl is the Microsoft C++ compiler driver; /EHsc selects the normal C++ exception-handling model, /std:c++17 requests the C++17 language mode, and /Fe:hello.exe names the executable. The compiler and linker may create intermediate files as well.

Run hello.exe from that same Developer Command Prompt only after a successful build. A normal Command Prompt or PowerShell may not know where cl and its headers are; use the developer prompt for compilation. If you already built the file and switched to PowerShell, type .\hello.exe to run the local executable there. In a Visual Studio Console App project, you can instead replace the generated source and use Build Solution followed by Ctrl+F5.

Visual Studio Developer Command Prompt, from the main.cpp folder

cl /EHsc /std:c++17 main.cpp /Fe:hello.exe
hello.exe

Read and correct a first compiler error

Try the deliberately broken version below in a copy of main.cpp: the output statement has no semicolon after the closing quotation mark. When you build, the compiler should stop and print an error near that line or the next one. GCC, Clang and MSVC word their diagnostics differently, so look for the filename, line number and the first relevant message rather than memorising one exact phrase.

Add the missing semicolon to the end of the std::cout line, save and build again. Once the build succeeds, run the executable and confirm the greeting appears. A failed build can leave an older executable in the folder; running that old file does not prove the error has been fixed. If the diagnostic instead says iostream cannot be found or cl/g++/clang++ is unknown, check your toolchain installation and terminal environment rather than changing the C++ syntax.

Deliberately broken main.cpp: missing semicolon after the output line

#include <iostream>

int main() {
    std::cout << "Hello, Codectionary!\n"
    return 0;
}

Practise with one small change

Restore the working program, change the greeting to a message of your own, save, rebuild and rerun. Then remove only the final return 0; and check that the program still builds and exits successfully: main has a special implicit-success rule. Put it back if you prefer an explicit result. These two experiments check that you understand the difference between source code, build output and runtime output.

C++17 is a stable, well-supported teaching baseline for this page, not the newest possible language mode. Newer standards add features, but compiler and standard-library support vary by toolchain and release. Learn the basics here first, then check your compiler’s feature support before selecting a newer -std= or /std: mode for a project.

Syntax

#include <iostream>

int main() {
    std::cout << "Hello, Codectionary!\n";
    return 0;
}

Examples

A complete greeting program

Save as main.cpp; build with the command for your operating system, then run the generated executable.

#include <iostream>

int main() {
    std::cout << "Hello, Codectionary!\n";
    return 0;
}
  1. The goal is to print one greeting and finish successfully. This is a complete C++ console program, so the file needs no surrounding code.
  2. #include <iostream> supplies the standard stream declarations. main is the entry function; its braces enclose the two statements that execute.
  3. std::cout << inserts the string into standard output. The \n escape moves output to a new line; it does not print a visible backslash and n.
  4. The semicolons end the output and return statements. return 0 reports success and may be omitted only because execution reaches the end of main.
  5. Save the file, build it, then run the new executable. A practical command-line tool uses the same stream pattern to report results or status.

Best practices

  • Build with one installed toolchain and an explicit C++17 mode; use the matching shell and run command for your operating system.
  • Read the first relevant compiler diagnostic, save the correction and rebuild before running.
  • Treat a successful build and the expected runtime output as separate checks.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading