As seen before, the best way to learn programming is by writing programs. One of the most common practices is to write a "Hello World" program, which prints "Hello World" in your screen.
First, create a new file with the following contents:
// My first program!
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Compile your program, when finished, the console screen should say:
Hello World!
Here is a screenshot of the ouput of your first program:
1. // My first program!
2. #include <iostream>
3.
4. int main()
5. {
6. std::cout << "Hello World!" << std::endl;
7. return 0;
8. }
// My first program
In C++, two slash signs mean that the rest of the line is a comment. Comments have no effect on the behaviour of the program.
Comments are used to explain what your code does to other programmers. Comments are also used to provide observations of a particular feature of your program, such as its license or documenation.
#include <iostream>
Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream>
instructs the preprocessor to include a section of standard C++ code, known as header iostream
, which allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen.
A blank line has no effect on the behaviour of the program. They are used for readibility purposes.
int main()
This line initiates the declaration of a function. Functions are sections of a program that perform a specific operation, such as launching your program or adding two numbers together.
Functions will be discussed in detail in a later chapter. Essentially, their definition is introduced with a succession of a type (int)
, a name (main
), and a pair of parentheses (()
), optionally including parameters.
The function named main
is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function, regardless of where the function is actually located within the code.
{
and }
The open brace ({
) at line 5
indicates the beginning of main's function definition, and the closing brace (}
) at line 7
, indicates its end. Everything between these braces is the function's body that defines what happens when main
is called. All functions use braces to indicate the beginning and end of their definitions.
std::cout << "Hello World!" << std::endl;
This line is a C++ statement. A statement is an expression that can actually produce some effect. It is the meat of a program, specifying its actual behavior. Statements are executed in the same order that they appear within a function's body.
This statement has three parts: First, std::cout
, which identifies the standard character output device (usually, this is the computer screen). Second, the insertion operator (<<
), which indicates that what follows is inserted into std::cout
. Finally, a sentence within quotes ("Hello world!"), is the content inserted into the standard output.
Notice that the statement ends with a semicolon (;
). This character marks the end of the statement, just as the period ends a sentence in English. All C++ statements must end with a semicolon character. One of the most common syntax errors in C++ is forgetting to end a statement with a semicolon.