02. CODE: Write and Run Your First C++ Program

Write and Run Your First C++ Program

Now that you have a sense of what you will be building in this lesson, you can learn about the tools that you will use. To get started, it helps to know a little bit about the C++ programming language. C++ is a compiled language; there is a separate program - the compiler - that converts your code to an executable program that the computer can run. This means that running a new C++ program is normally a two step process:

  1. Compile your code with a compiler.
  2. Run the executable file that the compiler outputs.

C++ Main()

In C++, every program contains a main function which is executed automatically when the program is run. Every part of a C++ program is run directly or indirectly from main , and the most basic program that will compile in C++ is just a main function with nothing else.

main() should return an integer (an int in C++), which indicates if the program exited successfully. This is specified in code by writing the return type, followed by the main function name, followed by empty arguments:

int main()

The body of the main() , which comes after the main function name and arguments, is enclosed in curly brackets: { and } . In this exercise, you will write the smallest possible C++ program, which is a main function with empty body. If you have trouble, have a look at the solution.cpp file in the workspace below.

Remember that you can compile and run your program with the following:

  1. To compile, use the following command: g++ main.cpp
  2. To run, use: ./a.out

To Complete This Exercise:

  • Write a main function in the main.cpp file below, and then compile and run the program. The program will not have any output, but it should compile and run without errors.


This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: generic
  • Opened files (when workspace is loaded): n/a
  • userCode:

    export CXX=g++-7
    export CXXFLAGS=-std=c++17
    g++() {
    /usr/bin/g++-7 -std=c++17 "$1"
    export -f g++