Namespaces
Variants
Views
Actions

Your first program in C++

From cppreference.com


The following is one of the most basic programs that is possible to write in C++. This should give you a first feel of how you can get a computer to do things for you. Be sure to click the Run this code button which will compile and run the code on the server.

#include <iostream>
 
int main()
{
    std::cout << "Hello World!\n";
    return 0;
}

Output:

Hello World!

Let's analyze the code line by line.

#include <iostream>
#include is a preprocessor directive that allows code to include functionality from somewhere else. In this case, the standard iostream library is included. It contains the declaration of std::cout, which we use later; without this line, the compiler would not recognize std::cout that appears later, and we would get an error.
int main()
This is the main function. The main function is a function that defines the user code which is run when the program is launched. Every program must have a function called main, otherwise the computer would not know where to start the program.
All function definitions have four parts:
  • A return type (in this case int), which specifies what value is returned to the code that calls the function.
  • A name (in this case main), which identifies the function.
  • A parameter list (in this case empty), which specifies what data can be passed to the function by the code that calls the function. In this case the parameter list is empty. The parameter list is always enclosed in parentheses.
  • A function body (in this case std::cout << "Hello world!\n"; return 0;) - a sequence of statements that identify the actions that the function will perform. The function body is always enclosed within curly braces ({ and }).
std::cout << "Hello World!\n";
This is the line where all the action of printing Hello World! to the screen is described. The line contains a single statement, which ends at ;. Let's examine what the statement does. std::cout is the name of special object that represents console output. << is an operator (like a + or / operators), which instructs the compiler to generate code to print to std::cout (console) whatever comes after the << operator. In this case this is the text "Hello world\n". The \n part is a special character that identifies a newline. Such characters are called escape sequences.
The std:: prefix of std::cout indicates that the object comes from the standard library. It is possible to save some typing and use std::cout without std::, as cout, but only if using namespace std; is present somewhere above. We advise against this practice, because the standard library contains many simple names, such as std::count, std::list, etc. In more complex programs, some of your own variables might be named as count or list, which will lead to ambiguities and hard to debug bugs in your program. It's better to always use std::. Omitting it doesn't save a lot of time, since as the time goes, you'll learn to type std:: very fast :)
return 0;
This is the return statement. At this point, the main function is terminated. 0 is the return value. In the case of main, the return value indicates to the operating system, whether the program has finished successfully or not. Here we return 0, which indicates successful execution.