C++ Programming Tutorial Lesson 01: Hello World!

The best way to learn computer programming is practice, practice, and more practice. So, let's get started! Learning how to program by example is one of the best methods possible. Below is one of the simplest programs you can create. It is typically referred to as the "Hello World!" example whereby the whole purpose of the program is to print out "Hello World!" onto the screen.

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   printf("Hello World!\n");
   system("PAUSE");
   return 0;
}
You can copy and paste the code into the compiler, Dev-C++ for example, or you can download the Hello World example source code directly. If you compile and run this program, then you should see a console window as depicted below that says "Hello World!" and then an indication that you can press any key to continue the program execution.



Congratulations! You've just created the Hello World program! Now, let's examine the code to understand how the program works.

Source Code Description & Explanation
#include <stdlib.h>
#include <stdio.h>
These are C++ include statments. They typically come at the top of the source code file and are used to include other C++ code, whether it be from the standard C++ library or from other source code files in your project. Here, the first line includes the file "stdlib.h", which is from the standard C++ library and allows us to use the system() function in our program. The second line includes the file "stdio.h", which is also from the standard C++ library and allows us to use the printf() function in our program.
int main(void) {
This is the main function. Functions are denoted by the parentheses, ( and ). In front of the main function is the term "int". This means that the main function will return an integer to the function or process that called it. Don't worry about this for the time being, simply note that the program must return an integer before the end. Inside the parentheses of the main function is the term "void". This means that the main function is not accepting any arguments, i.e. a call to the main function does not require any variables to be passed in this case. Again, don't worry about this for the time being. Your program will begin running here at the main function. The curly braces, { and }, contain the code within a function. At the end of the main function, signified by the } or end curly brace, the program terminates.
printf("Hello World!\n");
This is a call to the printf() function, which is part of the standard C++ library and is defined in stdio.h. The printf() function prints a formatted string to the screen. The string is contained by the double quotation marks " and ". In this case, the string is "Hello World!\n" where the "\n" represents a newline character, i.e. it tells the printf() function to create a new line after "Hello World!". The semi-colon at the end of the printf call is required by C++ after every command within a function. This is simply a C++ rule for syntax purposes. The compiler would give an error if this semi-colon were not present.
system("PAUSE");
This is a call to the system() function, which is part of the standard C++ library and is defined in stdlib.h. The system() function tells the operating system to run a command in the form of a string. In this case, the string is "PAUSE", which tells Windows or DOS to print "Press any key to continue . . ." and waits for the user to press a key. This is necessary to prevent the console window from disappearing before we can see the "Hello World!" output string in Windows. As with the printf() function call, C++ requires the semi-colon at the end of the call to the system() function.
return 0;
This is the last command in the main function, the return statement. Its purpose is only to return a value to the function or process that called the main function. For now, don't worry about this other than the fact that it is required by the "int" in front of the main function definition. Returning a zero from the main function means that the program ran successfully and exited.
}
The last line of the program is the end curly brace, }. As described above, this ends the encapsulation of the commands within the main function.

Copyright © 2008 Pierre Dufilie IV. All Rights Reserved.