First Program In C++ Programming Language Hello Word

#include <iostream.h>

using namespace std;

int main()

{
cout << ” Hello World “;
}

<<Hello>>

Line 1: #include <iostream.h>

The C++ compiler runs a program called the C++ preprocessor. The preprocessor is expert to set in and carry off code from your source file.

The directive #include tells the preprocessor to contain code from the file iostream. This file contains declarations for functions so as to the program needs to relate.

Line 2: using namespace std;

This allows variables to center to exact areas of code.

The command using namespace std permits all objects and functions from the standard input and output library to be used within this program.

Line 3: int main()

This statement declares the main function. A C++ program can hold many functions but must always have one main function.

A function is a self-sufficient module of code that can achieve some job. The ” int ” specifies the return type of main to be an integer. 0 is returned by default.

Line 4: {

This opening bracket denotes the start of the program.

Line 5: cout << ” Hello World\n”;

cout is a object from a standard C++ library that has a method used to print strings to the standard output.

The compiler relations code from these standard libraries to the code you have printed to construct the last executable.

The “\n” is a particular arrangement modifier so as to tells the practice to place a line nourish at the end of the line. If there were another “cout” in this program, its string would print on the next line.

Line 6: }

This closing bracket denotes the end of the program

An Introduction to the program block of C++

To view How to open Turboo C++,How to change Directory,Open & Create new files in Turbo C++ Click Here

To view animated explanation of C++ menus Click Here

Filename

A source file is the file that contains the C++ code that is compiled into an executable file, using a C++ compiler.

C++ source files usually have an extension name of cpp, or cxx, depending on the compiler.

Library Files

Like C, C++ has a limited number of keywords, and depends on Library Files for its functionality.

This functionality is achieved by using library files. These library files are referred to as Header files and have a .h extension name. The standard input/output header file is called iostream.h.

The #include statement is used to insert the header code so your program may use the functions defined in the header file.

Angled brackets are used to instruct the preprocessor to find the header files from the standard header file directory.

The following illustrates how to include the iostream.h file:

#include <iostream.h>

Comments

There are two types of comments in C++:

1. Block Comments:

Block comments span several lines. The characters /* start a block comment, and the characters */ end the block comment.
/*

This is a block comment. The body of the comment is placed over several lines. The comment ends when the terminating characters are provided. */

2. In-Line Comments:

An in-line comment is a comment that is on a single line. Inline comments do not require termination characters, as they’re automatically terminated at the end of the line.

The characters // are used to start an inline comment.
// This is an in-line comment

Nested block comments are not allowed in C++. The following causes an error.
/*

Start of a block comment.
/* An illegal nested comment */ End of a block comment.
/*

Inline comments may be nested within block comments, or even inline comments.
/*

Start of a block comment.
// A legal inline nested // comment End of a block comment.
*/

Preprocessor Directives

Before the program is compiled, the source code is scanned by a preprocessor to perform text substitution, and header file inclusions.

C++ supports inline functions, and constants, so is consequently more often used just for header file inclusion and conditional compilation statements.

Preprocessor directives are preceded with a #. The # should always be placed in the first column of the line.

The following is a directive that causes the contents of the header file to be brought into the current file, effectively replacing the directive line with the contents of that file.

#include <iostream.h>

The main Function

Most C++ programs have one main( ) function, with the exception of Dynamic Link Libraries (DLL’s) in Windows. The main( ) function is the starting point of the program. The ANSI C++ definition for declaring the main( ) function is either:

int main()
Or
int main(int argc, char **argv)

The second definition is used with Command Line Arguments and shall be covered later. The int keyword means that the program should return an integer (whole number) to the operating system, signifying success or failure.

A return value of zero indicates success. Each statement within the main function or any other functions should be tabbed in (indented) to show the structure.

The braces are used to group the statements within the function together.

The following is the basic structure of a C++ source file:

#include
int main()
{
// Statements belonging to the function are tabbed
return 0;
}

A sample C++ program “WELCOME TO EBIZ”:

Here’s the traditional first program for newcomers to C++. Create a new source file using your IDE or text editor and type in the code below:

Output of the program:

Analyzing the structure of the program:

Let’s take a look at the above program line by line. Firstly:

#include <iostream.h>

This is an include statement and tells the compiler to make the contents of the header available to the program.

The input and output streams cin and cout, used in our program to read keyboard input and output to the display screen, are defined in .

If you try to compile the code without including this header it will fail to compile.

The header is a standard library header and in common with all of the standard C++.

int main()

The above line is the start of the definition of function main(). All C++ programs must have a main()function which is the entry point to the program.

A typical C++ program in the real world will have many functions. In our simple case, we can put all of our code into the one function, main().

{

The opening brace, above, marks the start of a code block. In this case it marks the start of function main().

For every opening brace in a program there will be a corresponding closing brace marking the end of the code block.

cout << “WELCOME TO EBIZ” << endl;

This is where the real work is done in our simple program. Here the string “Hello, world” is output via the output stream, cout.

The endl manipulator outputs a newline and then flushes the stream. Because cout is buffered, output may not be displayed when first written. Flushing the stream forces the contents of the buffer to be output.

cin.get();

The line above causes the program to wait for input from the cin input stream.

This may not be required, however, there are some programming environments, for instance running a console program in a Microsoft Windows environment, where the output may disappear before you’ve had an opportunity to see it when the program completes.

Adding this line of code will enable you to see the output, and then press Enter to continue. If not required for your environment you can safely remove the line from your program.

return 0;

This is the return statement. Because main() is defined as returning an integer, the above statement will return a status to the calling process indicating normal program termination.

}

The closing brace, above, marks the end of function main

Running, Saving and Compiling the Program

Simply choose “New File” and type out the program coding.

After typing the code in the compiler, save the file by giving it some name. The “Save As” option will appear under the “File” menu. Give a name (for example: myfirstprg).

Now the file is saved as first.cpp. All C++ source files are saved in *.cpp format. Just like *.bmp represents a bitmap file, *.cpp denotes a C++ (C Plus Plus) source file.

To view How to edit & save C++ program Click Here

In the compiler program there will be an option called ‘Compile’ in the menu bar. Select the compileoption and the compiler will do its work.

It will compile the program and in case there are any errors, the compiler will point out the line where the error was detected. Check whether the program has been typed exactly as given earlier.

Even if a semi-colon is missing, it will lead to errors. If the compiler says no errors (or if the message “compiled successfully” appears), then you can go to the next stage: building the *.exe file.

To view How to Compile & Run C++ program Click Here

*.exe file extension stands for executable files. A *.cpp file cannot be run directly on the computer.

This has to be converted into a *.exe file and to do so select the “Make or Build exe” option in the compiler.

The file ‘first.exe’ will be created. Now the program can be executed from the DOS prompt by typing ‘first’. But instead of running the program every time from DOS, it will be convenient to run the program from the compiler itself and check the output.

In the compiler there will be another option called “Run”. Just click on this and the program will run from the compiler itself (you needn’t switch back and forth between DOS and the compiler screen).

What’s the problem?

When first.cpp is executed from the compiler the program will ask the user to enter a character.

Once a character has been entered, the program will return to the compiler screen. You won’t see your output! It might appear as if there is some problem with the program.

What happens is that the program displays the character on the screen, immediately terminates the program and returns to the compiler screen. This happens so fast that you can’t see the output being displayed.

Modify your program as shown below:

Out put of the program:

Only after a character is typed will the program move to the next instruction. In the above program, there is no statement other than return 0; after getch ( ). Hence program flow is as follows:

Output: The program asks the user to enter a letter. The user enters a letter. Then the program displays the typed letter on the screen.

The program will not quit at this point since there is a statement getch ( ). It will wait till the user types another character. When the user presses a character the program will quit.

Type the above program, save and run it from the compiler. Some compilers have named the function getch( ) as _getch( ). This function also requires the header.

In other compilers if a similar problem is encountered while displaying the result try using getchar( ); instead of getch( );

getchar( ) function doesn’t require the conio.h header file. If the compiler does not recognize getchar( ),then instead of adding any of the in-built functions, just add another line as follows:

cin>>letter;

at the end of the program just before return 0;

The program flow will be the same as described earlier.