tutorials.neonphog.com icon

c++ - Hello World

Hello World Code Compiling and Running It Walkthrough Conclusion

Hello World Code

hello.h

#ifndef HELLO_H
#define HELLO_H

int main();

#endif

hello.cpp

#include "hello.h"

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string sName;

	cout << "Hello World!" << endl;
	cout << "What is your name?" << endl << "> ";
	getline( cin, sName );
	cout << "Hello " << sName << "." << endl;

	return 0;
}

Compiling and Running It

1) Get the Source Code

Download "hello.cpp" and "hello.h".

2) Compile an Object File

Open up a shell (command line) to the directory where these two files are and run the following command:

g++ -c -o hello.o hello.cpp

You should now have what is called an object file named "hello.o". The "-c" option tells g++ not to go all the way through with linking the executable (we will do this manually next). The "-o" option allows you to specify what the output file should be named, in this case "hello.o". You then list all of the source files to be compiled, in this case, just "hello.cpp".

3) Link together the executable

To generate the executable:

g++ -o hello hello.o

You should now have an executable file named "hello" (on Windows, it will be named "hello.exe"). The options are very similar. "-o" gives the output name "hello". Then, you pass all the object files that will be linked together.

4) Run It!

Running the executable you just generated should give something in the form:

$ ./hello
Hello World!
What is your name?
> David
Hello David.
$

Walkthrough

C++ code (and C for that matter) tends to be broken up into two files. The first, known as a "header" file (extension ".h", or ".hpp") is for code that may need to be seen (included) by other source files. The other one (extension ".cpp") is normally where the actual code is placed.

Let us start with the header file:

1) Standard Header Preprocessor Directives

#ifndef HELLO_H
#define HELLO_H

/* ... header code here ... */

#endif
I like to follow the coding standard of making these an all-caps version of the filename, exchanging dots (.) for underscores (_). If the contents of the file are in a namespace, I put that there too, i.e. (TUTORIAL_HELLO_H).

Any line in C that begins with a pound sign (#) is what is known as a pre-processor directive. You will find the above three directives in almost all header files out there. They ensure that the code in your header file does not get duplicated, included multiple times.

2) The Function Declaration

int main();
I do not actually tend to make declarations for the "main" function, as it should not get called by any of your code, but this is just an example.

The purpose of this line is to tell all the code after it that there *will be* a function of this name (main) with these parameters (none) that returns this type (int). The compiler will make sure you hold up your end of this promise.

On to the source file:

3) Includes

#include "hello.h"

#include <iostream>
#include <string>
Use angle brackets (< >) for system includes and quotes (") for your own headers.
Warning: you should *never* include a cpp file directly. You will start to have all sorts of problems with things showing up more than once, etc...

You should get used to always including your own header file first, then any system headers you may need. In this case, we include <iostream>, which will give us the "cin" and "cout" streams allowing us to exchange data with the user, and <string>, which will let us store a string of characters in our program's memory.

4) Using Namespaces

using namespace std;

This line is a bit of a mixed blessing. Without it, everything that is in the namespace "std" would have to be prefixed with that namespace in order to be used. This includes "std::string", "std::cin", and "std::cout". Someday, however, you will forget to include this line, and spend some time wondering why all of a sudden your compiler doesn't know what "cout" is.

5) The Function Definition

int main()
{
	/* ... code goes here ... */
}

This is the actual definition of the "main" function that you declared in the header file. Between the curly brackets ( { } ), you will place the actual code that gets executed inside your function.

"main" is a special function in c and c++, it is the so-called entry point of your program when run as an executable. Basically, this is where you will start.

6) Declare a String Variable

	string sName;

This line will set aside some (static) memory where we can place a string of characters.

7) Display Some Text

	cout << "Hello World!" << endl;
	cout << "What is your name?" << endl << "> ";

Think of "cout" as the terminal that is the user of your program. "<<" is a stream operator indicating you are sending something to that terminal, and "Hello World!" is a literal string. You can stack up those operators on a single line. "endl" is a special variable that will send a newline ("\n") to "cout".

8) Get Some Input

	getline( cin, sName );

The "getline" function will acquire input from a stream ("cin") and place it in a string ("sName") until it reaches a new line (the user presses enter). Thus, whatever the user types in there prompt, until they hit enter, will get stored in our string variable "sName".

9) Send it back out

	cout << "Hello " << sName << "." << endl;

You can send string variables to cout as well as literals.

10) And we are done

	return 0;

"return" is a keyword that will send whatever expression follows it back to the function that called the current one. In this case, the integer "0" will be sent back to the operating system saying that this program was successful. Send something other than zero to indicate there was a problem.

Conclusion

You can now send and receive data to and from the console. In addition, this is the basic form which most of your c++ files will take.

Please check back for more advanced tutorials.

tutorials home c++ home