UCL WIKI

UCL Logo
Child pages
  • Program structure
Skip to end of metadata
Go to start of metadata

Perhaps the best way to learn a programming language is to dive in and see what it's all about with a simple example.

Here's out first program:

listing1.cpp
// My first C++ program

#include <iostream>

int main()
{
  std::cout << "Hello world!";
  return 0;
}

Hello world!

The left panel shows the program code and the right the output produced once the program has been compiled and executed.

This is one of the simplest programs that can be written in C++ and is the first program most people write when learning so you're walking in the footsteps of some of the world's best programmers!

Let's have a look at what's going on line by line:

// My first C++ program

This is a line comment. If at any point in a C++ program you use two consecutive forward slashes the rest of that line will be treated as a comment and won't be counted as part of the program.

Tip

Use comments liberally to remind yourself (or others) what that bit of code is meant to do. These will become invaluable when you come back to the code or share it with someone else.

#include <iostream>

Lines that begin with the hash symbol (#) denote a so called preprocessor directive i.e. a command telling the preprocessor to do something. The preprocessor scans your code immediately before your code is compiled (hence it's name) looking for these and acting one them. Only once the preprocessor is finished will the compiler do it's thing. This particular directive tells the preprocessor to find the file iostream (input/output stream) and read it in as if you had typed in the contents there yourself. iostream is a special file that is part of the standard template library that comes with almost all C++ distributions and will allows us to write text to the screen.

int main()

This line defines the main function. We'll see a lot more functions later but for now we need to know that main is special. Every C++ program must have a main function because this is where your program begins every time you run it. The int keyword before main stands for integer and tells you (and the operating system) to expect an integer to be returned when main finished. The curly brace ( { ) immediately after main tells the compiler where main begins, and the closing brace ( } ) on line 10 tells it where it ends.

std::cout << "Hello world!\n";

This is the meat of our program. The cout (pronounced see-out) object can be used to deal with output. In this case the output is being directed to the screen, but as we'll see later this could just as easily go to a file, or other output device. The std:: part tells the compiler to look for cout within (::) the std namespace. We'll cover namespaces later, but for now just remember that they are a way to group common elements of code together. << is called the redirection operator, it is part of cout and tells the compiler to send whatever comes after it to the screen. Then we have whatever we want to write in double quotes, this called in string i.e. a string of characters. The \n characters tell cout to put a new line at that point in the string. Finally the semicolon acts like a full-stop in English and tells the C++ compiler that we have finished that statement.

return 0;

This statement tells the compiler to come out of the main function immediately after this line returning the value 0. In this case nothing is done with the return value, but in general returning 0 from main means that everything went well while returning any other value can be an indication that something went wrong or something unexpected happened.

That's it! If everything went well you have written, compiled, executed and hopefully understood your first C++ program. Now have a play. Try changing the contents of the string to something else, or add another std::cout line and remove the \n character from the first to see what happens.

Useful information

Remember, every time you change the source of your program you must compile it again to see the changes.

Whitespace

Whitespace is the name given to characters used for formatting. In C++ this refers to spaces, tabs and (usually) newlines. In general whitespace is ignored by the C++ compiler and therefore the following versions of our code from above are all the same as far as the compiler is concerned:

// My first C++ program
#include <iostream>
int main() { std::cout << "Hello world!"; return 0; }
// My first C++ program
#include <iostream>
int main()
{
std::cout
<<
"Hello world!"
;
return
0
;
}

But just because the compiler doesn't care about whitespace doesn't mean that we shouldn't! Whitespace is important for layout out your code in a way that makes it easy for the eye to see what's going on.

Tip

Choose a style of laying out your code and try to stick with it so you get used to seeing certain things in particular places.

  • No labels