Recent Articles
Check Out
- Tech Today
- and our
- Computer Tutorials
- or join the discussion @ our
- Computer Forum
Search Articles
Your first C++ program
Dec 08 2011 06:24 PM | mr.d in Programming Tutorials
Everyone begins learning a new programming language with the "Hello World" program. This is the way a language is usually introduced and the program that helps explain the very first steps of beginning to code in that language. Today we are writing the "Hello World" program in C++ and taking a look at some of the syntax.//hello world
#include
using namespace std;
int main() {
cout << "Hello World" ;
return 0;
}
----------------------------------------------------------------
Let's begin.

//hello world
This is just a way of writing comments in your C++ code (and actually in many other languages). All text on a single line which is preceded with two forward slashes will not be compiled! The compiler ignores all text following the slashes on that line. Just remember that if you need to write a comment on more than one line of text, you will need to include the forward slashes at the beginning of each line!
#include
#include is a way of, get this, including code from other files right in your program. What it does, is place the code of a given file (iostream in this case) where it is being called. In this scenario we are including the standard C++ input/output library which allows us to read in data, and read out data. For including standard libraries we use angled brackets, for inclusion of your very own files it is customary to use double quotes (#include "myClass.h", myClass.h being a file we created containing code).
The difference between angled brackets and double quotes? When a file name is contained within double quotes, the compiler begins looking for the file in the directory containing the parent file (your program). You can also specify a directory, the compiler will stop looking if it doesn't find it there!
Angled brackets are best left for standard libraries only!
using namespace std;
This goes way beyond the scope of an introductory tutorial, but I will give you something anyway. If we had not declared that we are using the "std" (no, not what you think!) namespace, we would not be able to write cout << "Hello World"; but would rather have to write std::cout << "Hello World"; .
int main() {
Here we are actually defining a function called main. I will try to cover functions at some point, but for now know that every C++ program begins here!
Some FYI: int (in this case) is the datatype that the function main returns, and every function must have a return type... And if a function does not return anything, thenvoid is the return type! Every function name is followed by parenthesis in which you can set the function to accept parameters. As you can see main() does not accept any (although it can!). The curly bracket { marks the beginning of a block of code, if you look at the end of the program you can see the closing bracket } .
cout << "Hello World" ;
Without going into gory detail about the C++ language, cout is a way of outputting text to the command line! Everything contained within the double quotes will be written to the screen. Well, what if you want to double quote a word in your output? You can! You can precede the double quotes that you want printed with an escape operator \. What I mean is this: If you wanted your output to say Hello "World", you would have to do cout << "Hello \"World\" ;
\n - Just a line break. If you wanted to print Hello on one line and World on the next, you can use \n for a line break! cout << "Hello \nWorld"; will give youHello on the first line and World on the second line.
Are you yet wondering what the semicolon is for? It marks the end of a statement!
OK, now to finish off the program.
return 0;
I previously told you that the function main returns a type int. Here it is returning a value of type int (zero)! This is also the point of termination of a function, at which the control is passed back to the calling function (in this case that would be the OS). In simplest form this is where a function ends, and in our case where our main function (program) ends!
Hope this helps some of those just getting into the C++ language.
If you have any tips or suggestions on improving this article, shoot me a message or state it in the comments!
If you have any C++ questions, you can ask in our programming forum!











0 Comments