Hello World

// file : hello.C
#include <iostream.h>
main()
{
  cout << "Hello World" << endl;
}

Let's go through this line by line. Notice that the first line starts with //. This indicates that a comment follows. A comment is ignored by the compiler and thus can be anything you want it to be. In this case, I used it to put the name of the file at the top of the source code. This way, you can look at the examples of code and know exactly which file it is.

The line that starts with #include is necessary for just about all C++ programs. In C++, every line that starts with # is called a ''preprocessor directive'' - we will see more of them as the semester progresses. Without the #include, you cannot do the normal input or output operations. We'll see what exactly they are shortly. The line basically ``includes'' the ``input/output stream''. (Stream being a ``flow'' of data - whether it be input or output). The ``.h'' just tells you that it is a ``header'' file. As we continue this semester, we will see other header files being included. For example, it probably won't be too long before we see the ``math.h'' header file included in our programs.

The keyword main() is the start of the main function. The execution of your program starts with the first statement after the { that follows main(). It continues line by line until it finds the } that ends the main function. There are ways of making the program ``jump'' around, skip certain lines, and loop over certains lines again and again, so the execution isn't truly ``line by line''. So, the thing(s) that are executed by this program is the cout statement.

The cout statement is the way C++ prints things to the screen. You can print text, such as "Hello World", on the screen using the above method. "Hello World" is a character string. This is where spaces are meaningful. To get a tab character in the character string, you can do something like "Hello\tWorld". The \t is what creates the tab. To get spaces in there, just use "Hello World".

The endl; (and notice that it's is ``end-L'' not ``end-one'') at the end of the line prints a ``carriage return''. This is analogous to the <br> tag in HTML. There are other ways of printing a carriage return. Basically, you can put a \n in the character string to make a carriage return. We will see more examples of how to use cout in the following section.

Anyway, if you type the above program into a file using a text editor and save it as ``hello.C'', you can compile and run the program by typing the following at the UNIX prompt :

g++ -o hello hello.C
hello

Compile the program? That's what the g++ -o hello hello.C did. g++ is the name of the compiler. Just as you typed emacs to edit a file, you have use g++ to compile a C++ program. The -o hello part tells the compiler to write the object code (or executable code or binary code) to a file called ``hello''. The last part, hello.C is the name of the source code (or C++ code) file.

Running the program? That's what the hello did. Since the compiler compiled the program and named the executable, ``hello'', we can now type ``hello'' to execute the program.

If you had errors in your C++ file (the source code) then it may not compile correctly and it may not run. If this is the case, go back to the editor and figure out what you typed in incorrectly, then recompile and try to rerun your program. If you did it correctly, it should print out the message ``Hello World'' on your UNIX window. This is kind of a lame program, but we have to start somewhere.