When you start learning a programming language, your first task is to make sure you understand how to use the system and run your first program. The more trivial the program the better, because if a problem occurs there is no chance the problem is in the program itself: the problem must be in the system that runs the program, such as how it has been set up.
This page is here for historical interestL if you have been following the pages up to here everything stated here should be familiar and should be useful revision.
Everyone's first program does the following. It simply prints the message "Hello, world!" and halts.
As simple as this is, writing and running such as program tests a number of important things. It tests that your system is working OK, it tests that you can use the editor correctly and save your file in the right location, it tests that the compiler works, and it tests that you can successfully run the resulting program. So here is the program in C++, with the traditional message left unchanged.
// helloworld.cpp (click here to download)
/* Everyone's first program: prints a message to the screen Richard Kaye January 2017 for 2NP */ #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; }
I need to explain the main features of this program and what the various lines do.
// helloworld.cppis a comment and is ignored by the computer. It is there for human readers to know the name of the file that you are looking at, and I will name all my example programs this way. Comments in C++ are written with // (to end of line) or between /* and */. The second kind of comment can go over several lines, but the first kind can't. All comments are for human readers only and are ignored by the computer. You can see the other kind of comment as well where the purpose and author of this program is given. It is good style to always label your programs in this way.
#include <iostream>tells the compiler to load the iostream package that will manage input and output (io). It is usual for C++ programs to start by loading a number of packages in this way, and a common error is to forget to load a required package.
using namespace std;tells the compiler that everything declared in the loaded packages with names starting with
std::is to be directly available in this program. In this case the two relevant names are cout (for the output console) and endl (the carriage-return or end-of-line symbol). Without the
using namespaceline you could still have accessed these objects as std::cout and std::endl. Such a
using namespace std;is recommended for beginners to C++ but not for more advanced programmers, and especially not for long programs.
int main() { ... }defines a function called "main". Functions are always indicated by round brackets following the name of the function. In this case "main" takes no inputs (as indicated by the () part) and has an "int" (a kind of integer) output. The part between { .. } is the instructions telling the computer what the function should actually do. All programs must have a function called "main".
cout << "Hello, world!" << endl;does the work. It places (using the operator <<) the string "Hello, world!" and the end-line character endl on the console output cout so that it can be seen.
return 0;says that the function should stop and the value returned as the output. In this case (since this is "main") this return value communicates to the operating system the success of the operation. The convention here is that 0 means "OK" and any other number means "failure". We will always return 0 from main in this way. (It is possible in the operating system to get this number after the program has run, and, if a failure occurs, run a different program and so on. However, details of how to do this are outside the scope of these web pages.)
Exercise.
Type this program into an editor or IDE, compile and run it. Now change the message and re-run it and check the result is as you expect. See the web page on using Dev-C++.
Traps for beginners.
There are a number of potential traps for beginners here, mostly due to the fact that the syntax is precise and unforgiving. Pay attention to details! The reason that syntax is unforgiving is to ensure that the compiler cannot possibly misunderstand you and "guess" the wrong thing. In other words precise syntax will help you in the long run, but you must learn to be precise too!
For historical reasons things starting with # are not statements so #include <iostream> doesn't get a semicolon. The rules for when a semicolon is needed after } or ) are more complicated, and you will learn these as you need them. (Note: a semicolon is not needed after the body of a function you are giving, and a semicolon must not be placed between main() and the corresponding { that follows.)
Indentation of code is a useful quick visual way of showing what sections require what kinds of brackets to complete them and is strongly recommended. But note that the compiler does not itself pay any attention to the indentation and just because your code is indented does not mean the compiler will understand it in the way you intend.