By 25/02/2014 14 Comments

First C++ Program

The best way to learn C++ is to start coding right away. So here is our very first program in C++.

# include <iostream.h>
main()
{
cout << "Hello World";
}

# include <iostream.h>

#include: This is a pre-processor directive. It is not part of our program; it is an instruction to the compiler. It tells the C++ compiler to include the contents of a file, in this case the system file iostream.h. The compiler knows that it is a system file, and therefore looks for it in a special place. The features of preprocessor will be discussed later. For the time being take this line on faith. You have to write this line. The sign # is known as HASH and also called SHARP.

<iostream.h>

This is the name of the library definition file for all Input Output Streams. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. iostream.h is the name of the file in which has code to do that work for you.

main()

The name main is special, in that the main is actually the one which is run when your program is used. A C++ program is made up of a large number of functions. Each of these is given a name by the programmer and they refer to each other as the program runs. C++ regards the name “main” as a special case and will run this function first. If you forget to have a main function, or mistype the name, the compiler will give you an error.

Notice that there are parentheses (“( )”, normal brackets) with main. Here the parentheses contain nothing. There may be something written inside the parentheses. .

{ }

Next, there is a curly bracket also called braces(“{ }“). For every open brace there must be a matching close. Braces allows to group together pieces of a program. The body of main is enclosed in braces. Braces are very important in C++; they enclose the blocks of the program.

cout << “Hello World”

cout

This is known as output stream in C++ and C++. Stream is a complicated thing, you will learn about it later. Think a stream as a door. The data is transferred through stream; cout takes data from computer and sends it to the output. For the moment it is a screen of the monitor. Hence we use cout for output.

<<

The sign << indicates the direction of data. Here it is towards cout and the function of cout is to show data on the screen.

 “Hello World”

The thing between the double quotes (“ ”) is known as character string. In C++ programming character strings are written in double quotes. Whatever is written after << and within quotation marks will be direct it to cout, cout will display it on the screen.

;

There is a semicolon (;) at the end of the above statement. This is very important. All C++ statements end with semicolon (;). Missing of a semicolon (;) at the end of statement is a syntax error and compiler will report an error during compilation. If there is only a semicolon (;) on a line than it will be called a null statement. i.e. it does nothing. The extra semicolons may be put at the end but are useless and aimless. Do not put semicolon(;) at a wrong place, it may cause a problem during the execution of the program or may cause a logical error.






Next Tutorial →


← Previous Tutorial


Posted in: C++ Basics

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .