- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Hello Word

Posted by Tayyab



Right, now we can get into some programming! 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 we’ll look over it and delve into what it actually means.

1
2
3
4
5
6
7
8
9
#include <iostream.h>
using namespace std;

int main()
{
    cout << "Hello, World!";
    return 0;
}

Note: >> Second statement not allowed in TurboC++ 3.0 editor so please remove it if you use this example in TurboC++ 3.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 river’s 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 it’s 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 you’re confused it doesn’t matter, they aren’t really important now and we’ll 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 it’s 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 coutstatement 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.

Now that you've gotten the so-called source code of a program you need to compile it. We mentioned this previously in the previous section. If your program closes straight away once you run it, then try running it from a terminal/cmd prompt instead.
Well, that little explanation could have been a lot longer, but I think that will be enough for now.



<< Previous                                                                                                                 Next >>    

0 comments:

Post a Comment