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.
Run this code
#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 ofstd::cout
, which we use later; without this line, the compiler would not recognizestd::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 return type (in this case
- A name (in this case
main
), which identifies the function.
- A name (in this case
- 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}
).
- 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 (
-
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 tostd::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 ofstd::cout
indicates that the object comes from the standard library. It is possible to save some typing and usestd::cout
withoutstd::
, ascout
, but only ifusing namespace std;
is present somewhere above. We advise against this practice, because the standard library contains many simple names, such asstd::count
,std::list
, etc. In more complex programs, some of your own variables might be named ascount
orlist
, which will lead to ambiguities and hard to debug bugs in your program. It's better to always usestd::
. Omitting it doesn't save a lot of time, since as the time goes, you'll learn to typestd::
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 ofmain
, the return value indicates to the operating system, whether the program has finished successfully or not. Here we return0
, which indicates successful execution.