Program Listing 2-1 Myfirst.cpp
//Myfirst.cpp--Displays a message#include<iostream>//a preprocessor directiveintMain ()//function Header{//start of function body using namespaceStd//Make definitions Visiblecout <<"Come up and C + + me some time.";//messagecout << Endl;//start a new linecout <<"You won ' t regret it!"<< Endl;//More output}
If your program wants to use C + + input or output tools, provide two lines of code:
#include <iostream>
using namespace Std;
You can replace line 2nd with other code, which is intended to simplify the program.
C + + and C also use a preprocessor that processes the source files before the main compilation (as described in chapter 1th, some C + + programs use a translator program to convert C + + programs to C programs. Although the translator is also a preprocessor, the preprocessor is not discussed here, but only the preprocessor, which handles the compilation instructions with the name beginning with #. You do not have to perform any special actions to invoke the preprocessor, which runs automatically when the program is compiled.
#include <iostream>//a preprocessor directive
This compilation instruction causes the preprocessor to add the contents of the iostream file to the program. This is a typical preprocessor operation: Replace or add text before the source code is compiled.
This raises a question: Why add the contents of the iostream file to the program? The answer involves communication between the program and the outside world. Io in iostream refers to input (information entered into the program) and output (information emitted from the program). The input/output scheme for C + + involves multiple definitions in the iostream file. In order to use cout to display messages, the first program needs these definitions. #include编译指令导致iostream文件的内容随源代码文件的内容一起被发送给编译器. In fact, the contents of the iostream file will supersede the code lines in the program, # include <iostream>. The original file is not modified, but the source code file and the iostream are combined into a compound file, which is used by the next stage of compilation.
Understanding: When there are #include instructions in the program, the contents of the corresponding files, such as # include <iostream>,iostream, will all be read into the current file at the beginning of the compilation.
That is, the content in the iostream file will be replaced #include <iostream>.
C + + Primer Plus Learning notes 2.1.3 C + + preprocessor and iostream files