Step One Hello World
Where do we start? One of the common practices is to write a program that
will display "Hello World" on the screen. If you are successful, you will have
learned how to use the emacs editor and save your source code, how to use
the GNU c++ compiler, how to run the program you compiled, and just how big
the executable code size is for such a tiny program.
Sample program:
( 1) // Program Name: hello.cpp
( 2) // Description: Display something on the screen
( 3) // Author: Duck Dodgers
( 4) // Date: September 1, 2005
( 5)
( 6) #include <iostream> //this is needed for C++ I/O
( 7) #include <string> //this will be needed for strings later on
( 8) using namespace std;
( 9)
(10) int main(void) {
(11) //display something on a line by itself
(12) cout << "Hello world!" << endl;
(13) return 0;
(14) }
Anatomy of the program:
- The first four lines are comments--the // indicates a comment
which the compiler will ignore. // is a single line comment.
Everything
after the // on the same physical line will be ignored. There is also
a multi-line comment which begins with a /* and ends with a */. Multi-line
comments cannot be nested, however single line comments can be nested
inside of multi-line comments. The lesson to be learned here is to use
single line comments all of the time and reserve multi-line comments
for when you have to comment out a block of code which may include several
comments. Comments are good. Be nice and use them--let people including
yourself know what your program is doing. It is really depressing to
have to decipher someone else's code where the only comments are code
which is commented out.
- The fifth line is blank and is ignored by the compiler.
- The sixth line is a preprocessor directive. This line tells the
preprocessor to include a copy of the contents of the file iostream.h
in the file hello.cpp at this location. The resulting collection is
passed to the compiler. This directive must occur before any line of code
which uses the iostream library.
- The seventh line is like the sixth, except it is for strings
- The eighth line can make your life easier--it contains all of the standard
C++ library of objects, classes and functions.
- The ninth line is blank because it looks very nice to have a blank
line before and after each function
- The tenth line names the type of value to be returned by the function,
the function, and then its list of parameters. int means an integer is to be
returned and void means there are no parameters. main followed by an empty set of
parentheses () also usually means there are no parameters. In a conventional
C++ program, main is the first function called when the program is executed.
- The eleventh line is a comment--notice it is indented--pretty, huh?
- The twelfth line does all the work. cout is an
output stream object which
is defined in iostream.h and is normally associated with the computer display.
<< is an output operator. "Hello world!"
is a string literal which is
enclosed in double quotes. The operand endl is a
manipulator which is part of
the iostream library. It is a newline character which is placed into the output
stream.
- The thirteenth line has the function return 0 upon successful completion
- The fourteenth line is a closing curly brace which matches the one
on line ten. All of the statements between the curly braces make up the
main function.
After typing the program source code in using emacs (without the line numbers!),
save the file as hello.cpp and exit emacs. From the unix prompt, type:
c++ -o hello hello.cpp
c++ is the GNU C++ compiler.
The -o option means to name the executable file hello.
The C++ compiler compiles the source program called hello.cpp.
To execute the program after it successfully compiles, type ./hello
at the UNIX prompt. To see how big the program is, type ls -la to
see the size of the executable program called hello.
This size should be significant when you consider that the computer which got
astronauts to and from the moon started out with the equivalent of about 20,000 bytes
of storage!
Advance to Step Two
Return to the Table of Contents