Hello World in C++

Hello, and welcome to a C++ tutorial. I am also going to happily assume that this is your first c++ tutorial too. If not, I hope to improve your understanding of c++. I will attempt to do so as simple as humanly possible. Shall we get started then?

In order to make programs you will need something called a compiler. We will use g++. A compiler I have come to love. I use linux so it comes with my operating system. I am assuming you are using windows, so follow the rules from this website: G++.

Let us tick off the first check box on our list by telling you what we are making! By end of this lesson you will have a program (.exe) and when you double click it, a box will pop up and say ‘Hello World!’ and wait for the user to press enter. Of course it will then exit once the enter key is pressed.

Here is the code:

#include <iostream> //Allow us to use 'cout' and 'cin'
using namespace std; //Automatically add std:: before 'cout' and 'cin'
 
int main() { //All statements go inside here
  cout << "Hello World!"; //Print 'Hello World!' on the screen
  cin.ignore(); //Wait for enter to be pressed then exit
 
  return 0; //Explained later
} //End main(), end  statements

Lets move on to our next item on the list and I will explain all the //comment. These are comments, and are used to explain the code. The compiler (makes the .exe) ignores all the text after // until the next line. There is also another type of comment /* here */. This is called a block comment, often times it looks like this:

/******************************
* Author: Adam McKerlie      *
* Info: info info info        *
******************************/
 
//C++ code here

The extra asterisks(*) is only for a more simple look. Also, if you download and look at the source code, you will notice I used a block comment at the beginning of my code. Take a look at my comments on this page or the source code and you may notice that I have alot of line comments (//comment here), you should not have that many in your code (see “The Art of Commenting” for good commenting practices). The reason I have so many is becuase this is your first program (I assume) and it will make it much easier for you.

For now consider all the code except for cout << "Hello World!"; and cin.ignore(); you can use it in all your programs. A template I suppose you could call it. Incase you are confused still, the template would be:

#include <iostream>
using namespace std;
 
int main() {
  //Code here
 
  return 0;
}

Just put your statements where it says //Code here. My next tutorial will explain the template and why you need it. Alright, time to get a move on! Look at the source code and you will see some semi-colons(;). These just mark the end of a statement, so for every statement use a semi-colon at the end of it. Otherwise when you compile your source code you will get an error.

Now comes the juicy part of the program, so put your best foot forward and read on! Cout, and cin. These are the statements that make our program do something. First let me tell you about cout. Cout stands for ‘Console Output’ and the syntax (How you use it) is, cout << “Text to put on screen here”;. << is a stream operator, and is used for cout, no questions asked. After that you see qutation marks (”") which just say that this is text, not code. Tailing along is the semi-colon(;) which, as I stated earlier, signifies the end of the statement. So when you want to put text on the screen use cout << “Text here”; simple, eh?

Almost done here, just have to explain to you cin, it stands for ‘Console Input’. The syntax is cin << VARIABLE;. Variables will be explained later, in my variables tutorial (coming soon). You also will notice that the code we used is different! Our code, cin.ignore(); has cin then a dot, followed by ignore(). This says get console input then ignore it. It waits for the enter key to be pressed, but we do not want memory being used when it does not have to so we ignore it, and do not store it.

I also said I would explain return 0;. Well int main() { is a function, and every function returns a value. If the value returned is a non-zero number it means that it did not complete successfully. This is mainly used in if loops (In a future tutorial), so just except that is there, like everything else for now. It will all make sense in a few tutorials, just bare in with me.

Now to make the .exe. After saving the file as helloWorld.cpp, Open the command prompt (start -> run -> cmd) and locate to your saved file with cd. Lets say you saved it on your desktop, you would type cd /documents and settings/USERNAME/Desktop. If you type dir you should see everything that is on our desktop, listed in text, this includes helloWorld.cpp. Now once you are in the correct directory type g++ helloWorld.cpp -o hello. Assuming you got no errors, you should now have hello.exe in the same directory as helloWorld.cpp. Now type hello.exe or open the folder and double click on hello.exe. Either way it will say ‘Hello World!’ and wait for you to press enter.

If you like this post than why not add devjargon( ); to your Free RSS Updates, or sign up for Free Email Updates.

0 comments

  1. Get things started with the first comment.

Leave a Comment