|
1 2 3 4 5 6 7 8 |
//Printing Hellow World! #include <iostream> int main(){ std::cout << "Hello World! \n"; system("pause"); return 0; } |
Line begins with //, indicating that the remainder of the line is a comment. Programmers insert comments to document programs and also help people read and understand them. Comments do not cause the computer to perform any action when the program is run; they are ignored by the C++ compiler and do not cause any machine-language object code to be generated. The comment Text-printing program describes the purpose of the program. A comment beginning with // is called a single-line comment because it terminates at the end of the current line. C++ programmers also may use C’s style in which a comment possibly containing many lines begins with the pair of characters /* and ends with */.
|
1 |
#include <iostream> // allows program to output data to the screen |
This is a preprocessor directive, which is a message to the C++ preprocessor. Lines that begin with # are processed by the preprocessor before the program is compiled. This line notifies the preprocessor to include in the program the contents of the input/output stream header file . This file must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output.
CAUTION: Forgetting to include the header file in a program that inputs data from the keyboard or outputs data to the screen causes the compiler to issue an error message, because the compiler cannot recognize references to the stream components (e.g., cout).
|
1 |
int main() |
The parentheses after main indicate that main is a program building block called a function. C++ programs typically consist of one or more functions and classes. Exactly one function in every program must be main. Like our program above, it contains only one function. C++ programs begin executing at function main, even if main is not the first function in the program. The keyword int to the left of main indicates that main “returns” an integer (whole number) value. A keyword is a word in code that is reserved by C++ for a specific use. The left brace, {, must begin the body of every function. A corresponding right brace, }, must end each function’s body.
|
1 |
std::cout << "Hello World!\n"; // display message |
This instructs the computer to perform an action namely, to print the string of characters contained between the double quotation marks. A string is sometimes called a character string, a message or a string literal. We refer to characters between double quotation marks simply as strings. White-space characters in strings are not ignored by the compiler. std::cout, the << operator, the string “Hello World++!\n” and the semicolon (;), is called a statement. Every C++ statement must end with a semicolon (also known as the statement terminator). Preprocessor directives (like #include) do not end with a semicolon. Output and input in C++ are accomplished with streams of characters. Thus, when the preceding statement is executed, it sends the stream of characters Welcome to C++!\n to the standard output stream objectstd::cout which is normally “connected” to the screen.
If you have noticed that we placed std:: before cout. This is required when we use names that we’ve brought into the program by the preprocessor directive #include .
The << operator is referred to as the stream insertion operator. When this program executes, the value to the right of the operator, the right operand, is inserted in the output stream. Notice that the operator points in the direction of where the data goes. The characters of the right operand normally print exactly as they appear between the double quotes. Notice, however, that the characters \n are not printed on the screen. The backslash (\) is called an escape character. It indicates that a “special” character is to be output. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means newline. It causes the cursor (i.e., the current screen-position indicator) to move to the beginning of the next line on the screen.
|
1 |
system("pause"); |
This allows us to see the output of a function. If we don’t apply system(“pause”); on the function it directly exits the console.
|
1 |
return 0; // indicate that program ended successfully |
return 0 is one of several means we will use to exit a function. When the return statement is used at the end of main, as shown here, the value 0 indicates that the program has terminated successfully and the } indicated that the function is ended.


Thank you for writting this very useful Post.
I have used this code in my site.
Great work! I am looking for this function all over the web, it works great for me!
Great to hear it worked on you!
Thanks for this, great job!
there’s an error..