Every C++ program contains one or more functions, one of which must be named main. A function consists of a sequence of statements that perform the work of the function. The operating system executes a program by calling the function named main. That function executes its constituent statements and returns a value to the operating system.
每个 C++ 程序都包含一个或多个函数,而且必须有一个命名为 main。函数由执行函数功能的语句序列组成。操作系统通过调用 main 函数来执行程序,main 函数则执行组成自己的语句并返回一个值给操作系统。
Here is a simple version of main does nothing but return a value:
下面是一个简单的 main 函数,它不执行任何功能,只是返回一个值:
int main() { return 0; }
The operating system uses the value returned by main to determine whether the program succeeded or failed. A return value of 0 indicates success.
操作系统通过 main 函数返回的值来确定程序是否成功执行完毕。返回 0 值表明程序程序成功执行完毕。
The main function is special in various ways, the most important of which are that the function must exist in every C++ program and it is the (only) function that the operating system explicitly calls.
main 函数在很多方面都比较特别,其中最重要的是每个 C++ 程序必须含有 main 函数,且 main 函数是(唯一)被操作系统显式调用的函数。
We define main the same way we define other functions. A function definition specifies four elements: the return type, the function name, a (possibly empty) parameter list enclosed in parentheses, and the function body. The main function may have only a restricted set of parameters. As defined here, the parameter list is empty; Section 7.2.6 (p. 243) will cover the other parameters that can be defined for main.
定义 main 函数和定义其他函数一样。定义函数必须指定 4 个元素:返回类型、函数名、圆括号内的形参表(可能为空)和函数体。main 函数的形参个数是有限的。本例中定义的 main 函数形参表为空。第 7.2.6 节将介绍 main 函数中可以定义的其他形参。
The main function is required to have a return type of int, which is the type that represents integers. The int type is a built-in type, which means that the type is defined by the language.
main 函数的返回值必须是 int 型,该类型表示整数。int 类型是内置类型,即该类型是由 C++ 语言定义的。
The final part of a function definition, the function body, is a block of statements starting with an open curly brace and ending with a close curly:
函数体函数定义的最后部分,是以花括号开始并以花括号结束的语句块:
{ return 0; }
The only statement in our program is a return, which is a statement that terminates a function.
例中唯一的语句就是 return,该语句终止函数。
Note the semicolon at the end of the return statement. Semicolons mark the end of most statements in C++. They are easy to overlook, but when forgotten can lead to mysterious compiler error messages. | |
注意 return 语句后面的分号。在 C++ 中多数语句以分号作为结束标记。分号很容易被忽略,而漏写分号将会导致莫名其妙的编译错误信息。 |
When the return includes a value such as 0, that value is the return value of the function. The value returned must have the same type as the return type of the function or be a type that can be converted to that type. In the case of main the return type must be int, and the value 0 is an int.
当 return 带上一个值(如 0)时,这个值就是函数的返回值。返回值类型必须和函数的返回类型相同,或者可以转换成函数的返回类型。对于 main 函数,返回类型必须是 int 型,0 是 int 型的。
On most systems, the return value from main is a status indicator. A return value of 0 indicates the successful completion of main. Any other return value has a meaning that is defined by the operating system. Usually a nonzero return indicates that an error occurred. Each operating system has its own way of telling the user what main returned.
在大多数系统中,main 函数的返回值是一个状态指示器。返回值 0 往往表示 main 函数成功执行完毕。任何其他非零的返回值都有操作系统定义的含义。通常非零返回值表明有错误出现。每一种操作系统都有自己的方式告诉用户 main 函数返回什么内容。
使用命令 C:/directory> echo %ERRORLEVEL% 查看返回值
1.1.1. Compiling and Executing Our Program
1.1.1. 编译与执行程序
Having written the program, we need to compile it. How you compile a program depends on your operating system and compiler. For details on how your particular compiler works, you'll need to check the reference manual or ask a knowledgeable colleague.
程序编写完后需要进行编译。如何进行编译,与具体操作系统和编译器有关。你需要查看有关参考手册或者询问有经验的同事,以了解所用的编译器的工作细节。
Many PC-based compilers are run from an integrated development environment (IDE) that bundles the compiler with associated build and analysis tools. These environments can be a great asset in developing complex programs but require a fair bit of time to learn how to use effectively. Most of these environments include a point-and-click interface that allows the programmer to write a program and use various menus to compile and execute the program. Learning how to use such environments is well beyond the scope of this book.
许多基于 PC 的编译器都在集成开发环境(IDE)中运行,IDE 将编译器与相关的构建和分析工具绑定在一起。这些环境在开发复杂程序时非常有用,但掌握起来需要花费一点时间。通常这些环境包含点击式界面,程序员在此界面下可以编写程序,并使用各种菜单来编译与执行程序本书不介绍怎样使用这些环境。
Most compilers, including those that come with an IDE, provide a command-line interface. Unless you are already familiar with using your compiler's IDE, it can be easier to start by using the simpler, command-line interface. Using the command-line interface lets you avoid the overhead of learning the IDE before learning the language.
大多数编译器,包括那些来自 IDE 的,都提供了命令行界面。除非你已经很熟悉你的 IDE,否则从使用简单的命令行界面开始可能更容易些。这样可以避免在学习语言之前得先去学习 IDE。
Program Source File Naming Convention
程序源文件命名规范
Whether we are using a command-line interface or an IDE, most compilers expect that the program we want to compile will be stored in a file. Program files are referred to as source files. On most systems, a source file has a name that consists of two parts: a file namefor example, prog1and a file suffix. By convention, the suffix indicates that the file is a program. The suffix often also indicates what language the program is written in and selects which compiler to run. The system that we used to compile the examples in this book treats a file with a suffix of .cc as a C++ program and so we stored this program as
不管我们使用命令行界面还是 IDE,大多数编译器希望待编译的程序保存在文件中。程序文件称作源文件。大多数系统中,源文件的名字由文件名(如 prog1)和文件后缀两部分组成。依据惯例,文件后缀表明该文件是程序。文件后缀通常也表明程序是用什么语言编写的,以及选择哪一种编译器运行。我们用来编译本书实例的系统将带有后缀 .cc 的文件视为 C++ 程序,因此我们将该程序保存为:
prog1.cc
The suffix for C++ program files depends on which compiler you're running. Other conventions include
C++ 程序文件的后缀与运行的具体编译器有关。其他的形式还包括。
prog1.cxx prog1.cpp prog1.cp prog1.C
Invoking the GNU or Microsoft Compilers调用 GNU 或微软编译器The command used to invoke the C++ compiler varies across compilers and operating systems. The most common compilers are the GNU compiler and the Microsoft Visual Studio compilers. By default the command to invoke the GNU compiler is g++: 调用 C++ 编译器的命令因编译器和操作系统的不同而不同,常用的编译器是 GNU 编译器和微软 Visual Studio 编译器。调用 GNU 编译器的默认命令是 g++: $ g++ prog1.cc -o prog1 ---------------------------------------------------------- C:/c>g++ 1.4.cc -o 1.4 C:/c>1.4.exe 原因:生成文件名为:1.4(可能默认后缀是.4) 修改:g++ 1.4.cc -o 14 |
Running the Compiler from the Command Line
从命令行编译器
If we are using a command-line interface, we will typically compile a program in a console window (such as a shell window on a UNIX system or a Command Prompt window on Windows). Assuming that our main program is in a file named prog1.cc, we might compile it by using a command such as:
如果使用命令行界面,一般在控制台窗口(例如 UNIX 的 shell 窗口或 Windows 的命令提示窗口)编译程序。假设 main 程序在名为 prog1.cc 的文件中,可以使用如下命令来编译:
$ CC prog1.cc
where CC names the compiler and $ represents the system prompt. The output of the compiler is an executable file that we invoke by naming it. On our system, the compiler generates the executable in a file named a.exe. UNIX compilers tend to put their executables in a file named a.out. To run an executable we supply that name at the command-line prompt:
这里 CC 是编译器命令名,$ 表示系统提示符。编译器输出一个可执行文件,我们可以按名调用这个可执行文件。在我们的系统中,编译器产生一个名为 a.exe 的可执行文件。UNIX 编译器则会将可执行文件放到一个名为 a.out 的文件中。要运行可执行文件,可在命令提示符处给出该文件名:
$ a.exe
executes the program we compiled. On UNIX systems you sometimes must also specify which directory the file is in, even if it is in the current directory. In such cases, we would write
执行编译过的程序。在 UNIX 系统中,即使在当前目录,有时还必须指定文件所在的目录。这种情况下,键入:
$ ./a.exe
The "." followed by a slash indicates that the file is in the current directory.
“.”后面的斜杠表明文件处于当前目录下。
The value returned from main is accessed in a system-dependent manner. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo command. On UNIX systems, we obtain the status by writing
访问 main 函数的返回值的方式和系统有关。不论 UNIX 还是 Windows 系统,执行程序后,必须发出一个适当的 echo 命令。UNIX 系统中,通过键入如下命令获取状态:
$ echo $?
To see the status on a Windows system, we write
要在 Windows 系统下查看状态,键入
C:/directory> echo %ERRORLEVEL%
Exercises Section 1.1.1
|