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.
The escape sequence
\n (backward slash and the letter n) generates a new line in a text string.
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.
main()function houses all the instructions for the program.
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.
Using GNU, the compilation command is
g++ followed by the file name.
Here, the name of the source file is hello.cpp.
The execution command is
./ followed by the file name.
Here, the name of the executable file is a.out.
Single-line comments are created using two consecutive forward slashes. The compiler ignores any text after
// on the same line.
Multi-line comments are created using
/* to begin the comment, and
*/ to end the comment. The compiler ignores any text in between.
- 1There 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…
- 2Take 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…
- 3High 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:/…
- 4We 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…
- 5Woohoo! 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…
- 1C++ 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 …
- 2Compile: 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…
- 3Compile: 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…
- 4Programming 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…
- 5Congratulations! 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…