Examples of program

Example: output "Hello World!" On the screen.

#include <iostream> // use cin, cout, you must call iostream libraries, or compiler errors
#include <cstdlib> // use the system () call cstdlib library
a using namespace std;
int main ()
{
  cout << "the Hello World! "<< endl; // output" the Hello World "!
  system (" pause "); // paused, use the system (" pause ") calls cstdlib library
  return 0; // end of the process
}

Description:
1, with "//" at the beginning is comments, "//" after the contents of the input can not be used to explain the statement, enter the program; another annotation method is based on the C ++ language "/ *" indicates start of a comment with "* /" indicates the end of the comment, in between "/ *" and "* /" is the comment content.

2, # include <iostream>
tells the compiler preprocessor input standard header file output stream (the iostream) included in the program. This header contains statements as defined in the basic C ++ standard input and output library.

3, # include <> and #include "" difference of
<> is referenced library path of the compiler which header files
"" are referenced relative path to the header files in your program directory of
4, using namespace std using std (standard) namespace means, the so-called namespace is a mechanism in the standard C ++, used to control the conflict in different libraries. The use of classes or functions that can use the same name in different spaces.

5, int main ()
the behavior of the main function (main function) of the initial declaration. main () is the starting point of all running C ++ programs. Whether it is in the beginning of the code, the end or in the middle, this function code is always the first to be executed in the program starts running. All C ++ programs must have a main (), int main () int before Dev C ++ may be omitted, preferably in TC ++ and retained in VC ++.
behind the main with a pair of parentheses (), indicating that it is a function. All functions in C ++ is followed by a pair of parentheses (), the brackets may have some input parameters. Note that, even if nothing parentheses can not be omitted, as shown in the sample. SUMMARY main function () is, by a pair of braces {} parentheses.
6, cout << "! The Hello World" << endl;
cout is an output statement tells the computer to the string between the quotes on the (screen) standard output device.

cout is declared in the header file iostream in, so in order to use the header file iostream cout must be included at the beginning of the program. endl is a C ++ language linefeed control characters, the line feed display output representing the contents of the subsequent content.

7, system ( "pause")
at 4.99 the previous version of Dev C ++ environment, in order to see the result of the program, it is necessary before the return statement of the main function plus: system ( "pause"); when this program is run to the statement, the results show that the screen will stay, so that we have time to look at the output of the program, otherwise the result will flash across the display screen. After version 4.99 may not have to add the statement, operating results after the end of the display screen will automatically stay.

8, return 0;
main function main () return statement, is generally a function of the last executable statement. main () function using the return end. In the statement, a value of 0 indicates that the program successfully concluded, the other number indicates abnormal. In the latter case you will see a C ++ program ends with a similar statement.

9, in C ++, separate statements is a semicolon ";" as a separator, the branch just to make it easier to write code for people to read.

Guess you like

Origin blog.csdn.net/jiewu001/article/details/93460327