Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,326 Categories: 12

Total Tutorial Views: 41,477,249

C++ Beginner's Tutorial: Hello, World.

Tutorial Name: C++ Beginner's Tutorial: Hello, World.  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Comments: 0

Views: 2,491

Related Forum: PC Building Forum

Share:

C++ Beginners Tutorial: Hello, World.


Your first program will be the simplest and most widely used example in learning a programming language; writing a single phrase on the screen. Seem simple? It is, but is still much more complicated than just saying it aloud! First I shall present you with the code, and then well look over it and delve into what it actually means.


#include <iostream>
using namespace std;
 
int main()
{
    cout << "Hello, World!";
 
    return 0;
}


The first line tells the compiler that we want to link the iostream library to our program. A library is is a kind of repository that stores little snippets of code, called functions, that you can use. They also store other things too, such as variables and occasionally operators. More on all of that shortly. The iostream library contains code that allows us to send characters into a text stream, which is then fed into the screen. Think of it as an actual river; we are the rivers source and the screen is the sea. Whatever is put into the river from the source will go onto the screen. Maybe a bit over-analogised, but its still a good way of thinking about it. This library also lets us read this stream and modify it as it flows.

The next line tells the compiler that we want to use the namespace called std, which is the standard namespace. A namespace is a sort of area in which your code is stored. This is very useful if you want to have two pieces of code that have the same name. Normally pieces of code cannot share the same name, but if one is in one namespace and one is in the other then they can. They are in separated places, but come together in your code. If youre confused it doesnt matter, they arent really important now and well cover them in depth later. Since the iostream library uses the standard namespace, that is what we shall use.

The third line of code is the beginning of the actual code. It is the main code block, as its name implies. Everything inside those two braces ({}) is read and translated by the compiler. You will need this line in every program, because otherwise nothing will happen, and a program that does absolutely nothing is pretty useless!

Next comes the actual printing of our phrase, which in this case is Hello, World!. To do this we use some code that is stored inside the iostream library that we discussed earlier. This piece of code is called a statement, the cout, which sends some information into the stream that we discussed earlier. The << after it is called an operator. This one means output, so together with cout it means 'output the following to the stream'. The following in question is 'Hello, World!', and is called a string. A string is just a sequence of characters, and is shown by enclosing it inside double quotation marks. The cout statement doesn't just output strings though, as you will see later. It is much more powerful than that. Note that this line ends with a semi-colon (;). This signifies the end of a statement, and if not used the program will not compile.

The last line, not including the closing brace of the main code block, is simple, but important. It is always placed at the end of main (although it can be in other places as well) and tells the computer that the main block is finished; it returns a value of 0 to the CPU. A non-zero value usually denotes an error, so it is a very useful line.

Next tutorial will be on variables.

Ratings

Current rating: 7.00 by 6 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C++ Beginner's Tutorial: Hello, World." :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.