Learning of C++primer|section1.1 Learn to write simple C++ programs

charpter.1

1.1 Learn to write simple C++ programs

Every C++ program contains one or more functions , and there must be one named main .
A function consists of a sequence of statements that perform the function of the function .
The operating system executes the program by calling the main function, which executes the statements that make up itself and returns a value to the operating system.

Here is a simple main function that does nothing but returns a value:

 
 

int main() { return 0; }


The operating system uses the value returned by the main function to determine whether the program has completed successfully. A value of 0 is returned to indicate that the program has been successfully executed.

The main function is special in many ways, the most important of which is that every C++ program must contain a main function, and the main function is the (only) function that is explicitly called by the operating system.


Defining the main function is the same as defining any other function. A defining function must specify 4 elements:
Return type, function name, parameter list in parentheses (possibly empty), and function body.
The number of formal parameters of the main function is limited. The main function defined in this example has an empty parameter list. Section 7.2.6 describes other parameters that can be defined in the main function.

The return value of the main function must be of type int , which represents an integer. int type isBuilt-in type , that is, the type is defined by the C++ language.

Function body The last part of a function definition begins with curly braces and ends withStatements that end with curly bracesblock :

    {
        return 0;
    }
The only statement in the example is return , which terminates the function.

Note the semicolon after the return statement.
In C++ most statements end with a semicolon .
Semicolons are easy to ignore, and omission of semicolons will result in inexplicable compilation error messages.

When return takes a value (such as 0 ), this value is the return value of the function.
The return type must be the same as the return type of the function, or it can be converted to the return type of the function.
For the main function, the return type must be of type int , and 0 is of type int .

On most systems, the return value of the main function is a status indicator. A return value of 0 often indicates that the main function has completed successfully.
Any other non-zero return value has an operating system-defined meaning. Usually a non-zero return value indicates that an error occurred.
Every operating system has its own way of telling the user what the main function returns.

1.1.1. Compiling and executing programs

After the program is written, it needs to be compiled. How to compile depends on the specific operating system and compiler.
You'll need to check the reference manual or ask an experienced colleague for details on how the compiler you're using works.

Many PC-based compilers run in an integrated development environment (IDE), which bundles the compiler with associated build and analysis tools.
These environments are very useful when developing complex programs, but take a little time to master.
Often these environments contain point-and-click interfaces where programmers can write programs and use various menus to compile and execute programs. This book does not describe how to use these environments.

Most compilers, including those from IDEs, provide a command line interface. Unless you are already familiar with your IDE, it may be easier to start with a simple command line interface. This avoids having to learn the IDE before learning the language.

Program Source File Naming Convention
Program source file naming convention

Whether we use a command line interface or an IDE, most compilers expect the program to be compiled to be stored in a file. program file called source file .
On most systems, the name of a source file consists of the file name (eg prog1 ) and the file suffix. By convention, file suffixes indicate that the file is a program.
The file suffix also usually indicates what language the program was written in and which compiler was chosen to run. The system we use to compile the examples in this book treats a file with the suffix .cc as a C++ program, so we save the program as:
    prog1.cc


The suffix of a C++ program file depends on the specific compiler being run. Other forms also include.

 
   
prog1.cxx prog1.cpp prog1.cp prog1.C

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763954&siteId=291194637