1. Technology

Introduction to C++

Hello World

By

This is the first tutorial in the series Online C++ Tutorial for beginners. The next tutorial introduces variables and a way to count with them.

This is a rewriting of the original C++ tutorials into single page tutorials which can usually be run on either codepad.org or ideone.com.

Note these online services and others are provided free by third party providers so please don't abuse them. I've used them because they save you the hassle of installing a C++ compiler. You might consider doing that and running them instead because with many users hitting these services the response may get slow.

Let's Start with Hello World

Because it's a tradition for the first program in most computer languages. It shows off a minimal subset of the language features and is easy to explain.

For Absolute Beginners

Developing C++ programs is done by editing a text file that has a .cpp extension. So I might store this program in a file called hello.cpp.

Then the program is run through another program called a compiler that reads it and translates it into code that the computer can run directly. If I have made a mistake in the program, then the compiler will give a compile error and flag it. So I then have to edit it and re-compile it.

The online services provide an editor, compiler and let you run the program all in one. This is convenient and much easier than installing a compiler, then learning the commands to compile and run it. But not all programs can be run this way.

// Hello world, By David Bolton http://cplus.about.com
#include <iostream>

int main(int argc, char * args[]) {
  std::cout   return 1;
}

Running it on Ideone

Open up ideone.com in your browser and paste this Hello World link. It shows the source code in the middle and the result of the run. You can copy the source code to the clipboard using the copy to clipboard command or even clone it and play with it in ideone.

Line by Line explanation of Hello World

Here is the program shown with line numbers and an explanation of each line.

1 // Hello world, By David Bolton http://cplus.about.com
2 #include <iostream>
3
4 int main(int argc, char * args[]) {
5   std::cout 6   return 1;
7 }
  1. This is a comment. The compiler ignores everything after // which can be at the start of aline or on the end of a valid statement. After a // on a line the next line is read by the compiler. C++ also supports the multi-line C commentd which start /* and finish */. For every /* there must always be a corresponding */.

  2. The #include is called a compiler directive and tells the compiler to include a standard C++ library called iostream. Standard C++ libraries are always inside angle brackets < > but libraries you create should be inside double quotes "like this".

  3. You can use any amount of blank lines to make your program easier to read.

  4. All C++ programs have one function called main and that is where the program starts running from. Everything from this line to line seven is part of main. The int before main tells the compiler that main will return an int value and that's done in line 6. Ignore everything in the brackets for now, we'll come back to that when we look at function parameters. The code starts with the first brace on this line and ends with the matching closing brace on line seven.

  5. The cout operator is called to print out the text string "Hello World" followed by an endl (short for end line). The
  6. This line provides the return value from the main function. Again there is a semi-colon to terminate this statement.

  7. This is the closing brace of the main function (and also the program)

So all this does is output Hello World

Program Architecture

The compiler knows that when you include libraries, it has to build in a copy of the library into the program. The cout operator comes from the iostream library.

The library code for cout has already been compiled and is supplied as an object code file (code that the computer directly understands) and the last part of compiling that takes place is to take your code, plus any library code needed (like cout) and build it all into one exe. This is known as linking but only happens if your code compiled successfully.

This exe is loaded by your operating system (Windows, Linux or Mac OS X) into memory and executed (aka run).

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C++ Programming
  5. Learn C++ Programming
  6. Online C++ Programming Tutorial for Beginners: Lesson 1

©2014 About.com. All rights reserved.