Key Concepts

Review core concepts you need to learn to master this subject

New Line

std::cout << "Hello\n"; std::cout << "Hello again\n";

The escape sequence \n (backward slash and the letter n) generates a new line in a text string.

Program Structure

std::cout << "Hello\n"; std::cout << "Hello again\n";

The program runs line by line, from top to bottom:

  • The first line instructs the compiler to locate the file that contains a library called iostream. This library contains code that allows for input and output.
  • The main() function houses all the instructions for the program.

Terminating Statements

std::cout << "Hello\n"; std::cout << "Hello again\n";

In C++, the semicolon ; is used to terminate a statement.

Every statement must be ended with a semicolon, and failing to do so will result in errors thrown by the compiler.

Note: This is a very common error.

Basic Output

std::cout << "Hello\n"; std::cout << "Hello again\n";

std::cout is the “character output stream” and it is used to write to the standard output. It is followed by the symbols << and the value to be displayed.

Compile Command

std::cout << "Hello\n"; std::cout << "Hello again\n";

Using GNU, the compilation command is g++ followed by the file name.

Here, the name of the source file is hello.cpp.

Execute Command

std::cout << "Hello\n"; std::cout << "Hello again\n";

The execution command is ./ followed by the file name.

Here, the name of the executable file is a.out.

Single-line Comments

std::cout << "Hello\n"; std::cout << "Hello again\n";

Single-line comments are created using two consecutive forward slashes. The compiler ignores any text after // on the same line.

Multi-line Comments

std::cout << "Hello\n"; std::cout << "Hello again\n";

Multi-line comments are created using /* to begin the comment, and */ to end the comment. The compiler ignores any text in between.

Chevron Left Icon
Hello World
Lesson 1 of 2
Chevron Right Icon
  1. 1
    There are only two kinds of languages: the ones people complain about and the ones nobody uses. C++ is a programming language created by [Bjarne Stroustrup](https://news.codecademy.com/bjarne-st…
  2. 2
    Take a look at the hello.cpp file in the middle of the screen. It’s a C++ program! In our code editor, the file name is at the top: ![File](https://codecademy-content.s3.amazonaws.com/courses…
  3. 3
    High five! We just got your first program to run. C++, like most programming languages, runs line by line, from top to bottom. Here is the structure of a C++ program: ![Program Structure](https:/…
  4. 4
    We learned how to output a line of text with the following code: std::cout << “Hey Jude\n”; It will output: Hey Jude We can also output multiple lines by adding more std::cout statements: s…
  5. 5
    Woohoo! You have written your first C++ programs.  –> In this lesson, you have learned: - C++ is a general purpose coding language. - C++ runs line by line, from top to bottom. - std::cout is…
  1. 1
    C++ is a compiled language. That means that to get a C++ program to run, you must first translate it from a human-readable form to something a machine can “understand.” That translation is done by …
  2. 2
    Compile: A computer can only understand machine code. A compiler can translate the C++ programs that we write into machine code. To compile a file, you need to type g++ followed by the file n…
  3. 3
    Compile: Sometimes when we compile, we want to give the output executable file a specific name. To do so, the compile command is slightly different. We still need to write g++ and the file name…
  4. 4
    Programming is often highly collaborative. In addition, our own code can quickly become difficult to understand when we return to it — sometimes only a few hours later! For these reasons, it’s ofte…
  5. 5
    Congratulations!  In this lesson, you have learned: - Compilation and execution using the terminal. g++ hello.cpp -o hello ./hello - Single line comments can be created using //. - Multi-li…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo