示例程序Example Program

下面的 C 源程序包括两个源文件。The following C source program consists of two source files. 它尽可能在 C 程序中提供一些各种声明和定义的概述。It gives an overview of some of the various declarations and definitions possible in a C program. 本书后面的节中描述了如何写入这些声明、定义和初始化,以及如何使用 C 关键字(例如,static 和 extern)。Later sections in this book describe how to write these declarations, definitions, and initializations, and how to use C keywords such as static and extern. 在 C 标头文件 STDIO.H. 中声明 printf 函数。The printf function is declared in the C header file STDIO.H.

假定 mainmax 函数在单独的文件中,且程序的执行开始于 main 函数。The main and max functions are assumed to be in separate files, and execution of the program begins with the main function. 未在 main 之前执行任何显式用户函数。No explicit user functions are executed before main.

/*****************************************************************  
                    FILE1.C - main function  
*****************************************************************/  

#define ONE     1  
#define TWO     2  
#define THREE   3  
#include <stdio.h>  

int a = 1;                       // Defining declarations      
int b = 2;                       //  of external variables      

extern int max( int a, int b );  // Function prototype          

int main()                       // Function definition         
{                                //  for main function          
    int c;                       // Definitions for      
    int d;                       //  two uninitialized  
                                 //  local variables  

    extern int u;                // Referencing declaration     
                                 //  of external variable       
                                 //  defined elsewhere          
    static int v;                // Definition of variable      
                                 //  with continuous lifetime   

    int w = ONE, x = TWO, y = THREE;  
    int z = 0;  

    z = max( x, y );             // Executable statements      
    w = max( z, w );  
    printf_s( "%d %d\n", z, w );  
    return 0;  
}  

/****************************************************************  
            FILE2.C - definition of max function  
****************************************************************/  

int max( int a, int b )          // Note formal parameters are     
                                 //  included in function header   
{  
    if( a > b )  
        return( a );  
    else  
        return( b );  
}  

FILE1.C 包含 max 函数的原型。FILE1.C contains the prototype for the max function. 这种声明有时称为“前向声明”,因为该函数在使用之前被声明。This kind of declaration is sometimes called a "forward declaration" because the function is declared before it is used. main 函数的定义包含对 max 的调用。The definition for the main function includes calls to max.

#define 开始的行是预处理指令。The lines beginning with #define are preprocessor directives. 这些指令告知预处理器通过 FILE1.C.用数字 ONETWOTHREE 分别替换标识符 123These directives tell the preprocessor to replace the identifiers ONE, TWO, and THREE with the numbers 1, 2, and 3, respectively, throughout FILE1.C. 但是,此类指令不适用于 FILE2.C,将单独对其进行编译,然后将其与 FILE1.C 链接。However, the directives do not apply to FILE2.C, which is compiled separately and then linked with FILE1.C. #include 开始的行告知编译器包含文件 STDIO.H,该文件包含 printf 函数的原型。The line beginning with #include tells the compiler to include the file STDIO.H, which contains the prototype for the printf function. 预处理器指令在《预处理器参考》中进行了解释。Preprocessor directives are explained in the Preprocessor Reference.

FILE1.C 使用定义声明初始化全局变量 abFILE1.C uses defining declarations to initialize the global variables a and b. 声明但不初始化局部变量 cdThe local variables c and d are declared but not initialized. 为所有这些变量分配存储。Storage is allocated for all these variables. 静态和外部变量 uv 将自动初始化为 0。The static and external variables, u and v, are automatically initialized to 0. 因此,仅 abuv 在被声明时包含有意义的值,因为它们被显式或隐式初始化。Therefore only a, b, u, and v contain meaningful values when declared because they are initialized, either explicitly or implicitly. FILE2.C 包含 max 的函数定义。FILE2.C contains the function definition for max. 此定义满足 FILE1.C. 中对 max 的调用。This definition satisfies the calls to max in FILE1.C.

生存期、范围、可见性和链接中讨论了标识符的生存期和可见性。The lifetime and visibility of identifiers are discussed in Lifetime, Scope, Visibility, and Linkage. 有关函数的详细信息,请参阅函数For more information on functions, see Functions.

请参阅See Also

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