Getting Started

Creating and Running Your First C++ Program

All C++ programs must have a starting point for execution. This is a special function called main. When you run a program, the operating system calls main, executes the statements in it in order, then terminates the program when main terminates.

And the smallest complete program that follows this rule is:

int main()
{
    return 0;
}

The line int main() tells the compiler that we are declaring a function called main that will return an int. The curly brackets are the beginning and end of the body of the function, the part of the program in which the statements we want to run are written. The single statement return 0; sends the 0 back to the operating system.

Convention is that a return value of 0 indicates the program executed successfully.

If this function is omitted, or if it is not called main, then the program will not link and cannot be run.

A program that doesn't do anything is very boring. The first thing most programs do is print out a message. The standard library utility which does this is called std::cout. To use it, a program must include the relevant header.

#include <iostream>

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

The directive #include <iostream> causes the preprocessor to insert the declarations necessary to perform input and output. Without that line, the compiler would be unaware of the meaning of the name std::cout and would issue an error. The function call within main causes the text between the quotation marks to be sent to the console; the symbol << is the insertion operator, which takes whatever text appears to the right of it and inserts it into the output stream on the left of it.

The characters in the special sequence \n are sent in addition to the following output so that the display cursor is moved to the next line for anything printed afterwards.

Once the string has been output, return 0; again signals that the program completed successfully.

The above program can also be written with the message split into pieces. Every insertion can be done by its separate statement.

#include <iostream>

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

The first line of code writes Hello, to the screen, with a space after the comma. The second one writes world! followed by a newline.

Since we write to the same stream (standard output), the two portions end up side by side on the screen and the output is still Hello, world!.

This does not affect the behavior, by the way; it just demonstrates that each use of std::cout is a statement terminated with a semicolon.

A common mistake to make early on is omitting a semicolon. The semicolon indicates the end of a statement to the compiler. If you forget a semicolon, the compiler will read onto the next line and then give an error message that can often be quite far from the actual source of the problem. Insert the semicolon in the correct location and the problem is fixed.

Missing the return statement is another common mistake. Currently, in C++, a main function is permitted to fall off the end of its scope without an explicit return statement; the compiler will do it internally for you. In the spirit of making success explicit to a human reader, though, it is recommended to explicitly add the return statement while learning.

Once compiled and run, the operating system will load the executable, call main, execute all the statements in order, and finally receive the integer that main returns. If that value is 0, the run is considered successful; any other value can be used to indicate an error has occurred. For now, the only value you need is 0.

From these things, the required function main, the inclusion of <iostream>, the use of std::cout, the insertion operator, and the return of zero, make up the skeleton of every console program you will write for quite a while. With the skeleton in place, you can start stuffing in more and more statements, computing, reading, or deciding anything you want, all within the same main function called by the operating system at the start of every execution.

Exercise: Creating and Running Your First C++ ProgramStarter code, hints and solutionOpenClose
Guided practiceBeginner · 10–15 min

Practice: Creating and Running Your First C++ Program

Work through the mission, reveal hints only when needed, and compare your result with the model solution.

Your challenge

Change the second greeting string so the program greets Ada instead of world. Predict the complete line, including its space and newline.

Mission checklist

  1. Run the starter as a baseline, then predict the change requested in Your challenge.
  2. Make that change in your editor; use the lesson-specific hints if you need help.
  3. Compile and run your answer, then compare with the worked solution and explain the result.

Starter program

This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.

#include <iostream>

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

Hints

Hint 1

The two std::cout statements write to the same line until the second string reaches its \n. Keep the space after "Hello," when joining the text on paper.

Hint 2

To personalize the greeting, change the text inside the second pair of double quotes. Keep the closing quote, semicolon, and \n intact.

Worked solution

I changed the second greeting from world to Ada. The first insertion still ends with a space, so the two statements produce Hello, Ada! on one line.

#include <iostream>

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

Example output

Hello, Ada!

0 comments

No comments yet. Be the first.

Add a comment

About CppLearn

CppLearn is an interactive, free reference for modern C++. In each lesson, you'll find out how the language works, rather than just lines of code that you can copy and paste. We cover concepts starting from beginner lessons on fundamentals and proceed to templates, move semantics, smart pointers, and areas that usually trap or scare C++ learners.