[C++ Entry Guide] (03) Hello World

insert image description here

Probably the first program of every programmer is Hello World, this is where the dream begins. This article is the third part of C++ entry guide, let us start with the most classic Hello World.

look at the code

#include <iostream>

using namespace std;

int main()
{
    
    
    cout << "Hello, world!" << endl;
    
    return 0;
}
  • #include <iostream>
    The #include directive is used to include header files used by the program. This tells the compiler that it wants to use the iostream library, and the name in angle brackets indicates the name of the header file. It should be noted that the #include directive and the name of the header file must be placed on the same line. We generally put the #include directive at the beginning of the source file. In addition, <> generally refers to standard library files. If you use your own header files, you generally use "" instead of <>. For example: #include "myHeader.h", "" is what you want to use Header file location, path can be added. If no path is added, the default is the current file path.

  • using namespace std;
    Use standard namespaces. The using keyword is generally used to declare a namespace, also known as using declaration (using declaration). The form of using declaration is as follows:

  • int main(){}
    Each C++ program must contain one or more functions, one of which must be named main, and the operating system runs the C++ program through main, which is the program entry point . The definition of a function is divided into four parts, return type (return type), function name (function name), formal parameter list (parameter list), function body (function body). Here the return type is int, and the function name is main , the formal parameter list is empty, and the function body is the content enclosed by {}.

The return type of the main function must be int, that is, an integer type, and the int type is a built-in type, that is, a type defined by the language itself.

  • cout << "Hello World" << endl;
    cout Standard output object, the name is defined under the std namespace. cout is the standard output (standard output), corresponding to it is the standard input (standard input) cin. In addition, there are cerr and clog that are not commonly used. As the name implies, you can basically know what it is by looking at the name Well.
    << is the output operator , this operator accepts two operands, the object on the left must be an ostream object, and the operator on the right is the value to be printed.
    endl is a manipulator , which means newline. Same as escape character "\n".

  • return 0;
    The return value of the main function.

In most systems, the return value of the main function is used to indicate the status, returning 0 means success, and the meaning of the return value other than 0 is defined by the system, usually used to indicate the error type.

egg

insert image description here

Guess you like

Origin blog.csdn.net/duoduo_11011/article/details/130603746