You are here:Home » C++ » Hello World in C++

Hello World in C++

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 your 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: Parry Programmer
( 4)   // Date: September 1, 2010
( 5)
( 6)   #include <iostream>  //this is needed for C++ I/O
( 7)   using namespace std;
( 8)
( 9)   int main(void) {
(10)     //display something on a line by itself
(11)     cout << "Hello world!" << endl;
(12)     return 0;
(13)   }
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 can make your life easier--it contains all of the standard C++ library of objects, classes and functions.
  • The eighth line is blank because it looks very nice to have a blank line before and after each function
  • The ninth 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 tenth line is a comment--notice it is indented--pretty, huh?
  • The eleventh 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 twelfth line has the function return 0 upon successful completion
  • The thirteenth line is a closing curly brace which matches the one on line nine. All of the statements between the curly braces make up the "main" function.
After typing the program source code in using your edigtor (without the line numbers!), save the file as hello.cpp. From a terminal window, 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 prompt. To see how big the program is, type ls -la to see the size of the executable program called hello.

0 comments:

Post a Comment