Hello World!

Monday, June 20, 2011 1 Comments A+ a-

Hello World! Our first C++ program


The best way to start learning C++ is to write our first program - Hello World. We will present the basic parts of a running program.

/*
Our first C++ program: Hello World
Print "Hello World!"
*/

#include <iostream>

using namespace std;

int main(void)
{ 
 //print "Hello World!"
 cout << "Hello World!" << endl;
  
 getchar();

 return 0;

}



I. Comments:


/*
Our first C++ program: Hello World
Print "Hello World!"
*/
//print "Hello World!"
Lines 1-4 denotes a comment multiline comment (enclosed inside /*  */  symbols ) while Line 12 shows a single line comment (starts with // symbol). Although a program runs without a comment, it is very important to have them as documentation and guide. Comments will be very helpful in documentation and tracing especially for complex programs and software project. Some of its uses are to document what a program is all about, what a function does and what a certain variable is intended for.

II. The preprocessor directive:

#include <iostream>

The line which starts with a hash sign (#) are the preprocessor directives. They are indication for the compilers' preprocessor to include the iostream standard file. The iostream standard file specifically contains the basic standard input and output library of C++. They are other files which can be included in a program which depends if it is used within that program. In this program, iostream is included because we are using "cout", the standard output stream which is declared in iostream.

III. Namespace:

using namespace std;

This part is the declaration of usage of a namespace. C++ standard library elements are declared within a namespace called std.

IV. Main function:

int main(void){}

This is main function. All C++ programs starts executing with this function. Where ever this function is placed, even if it is in between other functions, a C++ program will always start with this function. Inside the curly brackets are all contained with the main function.
The declaration indicates that it returns an int (integer) value and accept no arguments (void).

VI. C++ Statements:

cout << "Hello World!" << endl;

This is an example of a statement. A statement is an executable unit within a program that does something. The statement above indicates to print the string Hello World! into the screen through the use of "cout" which inserts a sequence of characters into the standard C++ output stream." endl" indicates a new line so the cursor will be position in the next line.

getchar();

This line will ask for a character input. You can use this function when you want to ask for a character input and process the character. But in here, it is added just to ask a character input and let the screen stay before the output window will close. 

return 0;

This statement indicates the return value of the main function as indicated in its declaration that it will return an int (integer) value, in this case, 0. 

Check out the output of the Hello World program:


1 comments:

Write comments
suman
AUTHOR
March 21, 2019 at 7:25 AM delete

nice article for beginners.thank you.
welookups C++
javacodegeeks

Reply
avatar