源文件和源程序Source Files and Source Programs

源程序可以分为一个或多个“源文件”或“翻译单元”。A source program can be divided into one or more "source files," or "translation units." 编译器的输入称为“翻译单元”。The input to the compiler is called a "translation unit."

语法Syntax

translation-unit:translation-unit:
external-declarationexternal-declaration

translation-unit external-declarationtranslation-unit external-declaration

external-declaration:external-declaration:
function-definitionfunction-definition

declarationdeclaration

声明概述提供了 declaration 非终止符的语法,并且《预处理器参考》解释了翻译单元的处理方式。Overview of Declarations gives the syntax for the declaration nonterminal, and the Preprocessor Reference explains how the translation unit is processed.

备注

有关 ANSI 语法约定的说明,请参阅 C 语言语法摘要的简介。See the introduction to C Language Syntax Summary, for an explanation of the ANSI syntax conventions.

翻译单元的组件是包括函数定义和标识符声明的外部声明。The components of a translation unit are external declarations that include function definitions and identifier declarations. 这些声明和定义可以位于源文件、头文件、库和程序需要的其他文件中。These declarations and definitions can be in source files, header files, libraries, and other files the program needs. 必须编译每个翻译单元,并将生成的对象文件链接起来以生成程序。You must compile each translation unit and link the resulting object files to make a program.

A C“源程序”是指令、杂注、声明、定义、语句块和函数的集合。A C "source program" is a collection of directives, pragmas, declarations, definitions, statement blocks, and functions. 若要成为 Microsoft C 程序的有效组件,每个组件必须具有本书中描述的语法,但它们可以按照程序中的任何顺序显示(受本书中概述的规则的限制)。To be valid components of a Microsoft C program, each must have the syntax described in this book, although they can appear in any order in the program (subject to the rules outlined throughout this book). 但是,这些组件在程序中的位置会影响变量和函数在程序中的使用方式。However, the location of these components in a program does affect how variables and functions can be used in a program. (有关详细信息,请参阅生存期、范围、可见性和链接。)(See Lifetime, Scope, Visibility, and Linkage for more information.)

源文件无需包含可执行语句。Source files need not contain executable statements. 例如,您可能发现将变量的定义放置在一个源文件中,然后在使用这些变量的其他源文件中声明对这些变量的引用会很有用。For example, you may find it useful to place definitions of variables in one source file and then declare references to these variables in other source files that use them. 必要时,可通过此方法轻松查找和更新定义。This technique makes the definitions easy to find and update when necessary. 由于相同的原因,常量和宏通常会被归类为称为“包含文件”或“头文件”的独立文件中,可在源文件中将这些文件引用为所需文件。For the same reason, constants and macros are often organized into separate files called "include files" or "header files" that can be referenced in source files as required. 有关包含文件的详细信息,请参阅《预处理器参考》。See the Preprocessor Reference for information about macros and include files.

请参阅See Also

程序结构Program Structure