Cprogramming 简明教程

C - Program Structure

C language 中的典型程序具有某些强制部分和一些可选部分,具体取决于程序的逻辑、复杂性和可读性。通常,C 程序以一个或多个 preprocessor directives (#include 语句)开头,并且必须具有作为程序入口点的 main() function 。另外,可能有 variablesfunctions 、宏、其他用户定义函数等的全局声明。

A typical program in C language has certain mandatory sections and a few optional sections, depending on the program’s logic, complexity, and readability. Normally a C program starts with one or more preprocessor directives (#include statements) and must have a main() function that serves as the entry point of the program. In addition, there may be global declarations of variables and functions, macros, other user-defined functions, etc.

The Preprocessor Section

C compiler 带有几个库文件,扩展名为 “.h”。“h” 文件(称为“头文件”)由一个或多个预定义函数(也称为“库函数”)组成,将在 C 程序中使用。

The C compiler comes with several library files, having ".h" as an extension. A ".h" file (called a "header file") consists of one or more predefined functions (also called "library functions") to be used in the C program.

必须在任何 C 程序中加载库函数。 "#include" 语句用于包含头文件。它是一种 "preprocessor directive"

The library functions must be loaded in any C program. The "#include" statement is used to include a header file. It is a "preprocessor directive".

例如, printf()scanf() 函数需要执行控制台 I/O operations 。它们在 stdio.h file 中定义。因此,你总能在任何 C 程序的顶部找到 #include <stdio.h> 语句。其他重要且经常使用的头文件包括 string.hmath.hstdlib.h 等。

For example, printf() and scanf() functions are needed to perform console I/O operations. They are defined in the stdio.h file. Hence, you invariably find #include <stdio.h> statement at the top of any C program. Other important and frequently used header files include string.h, math.h, stdlib.h, etc.

还有其他预处理器指令,如 #define ,用于定义常量和宏,以及 #ifdef 用于条件定义。

There are other preprocessor directives such as #define which is used to define constants and macros and #ifdef for conditional definitions.

以下语句定义了常量 PI −

The following statement defines a constant PI −

#define PI 3.14159

Example

一旦常量被定义,就可以在 C 程序的其余部分使用它。

Once a constant is defined, it can be used in the rest of the C program.

#include <stdio.h>
#define PI 3.14159
int main(){
   int radius = 5;
   float area = PI*radius*radius;
   printf("Area: %f", area);
   return 0;
}

执行此代码后,您将获得以下输出 −

On executing this code, you will get the following output −

Area: 78.539749

你还可以使用 "#define" 指令来定义宏。它类似于 C 语言中的函数。我们可以将一个或多个参数传递给宏名称,并在代码段中执行操作。

You can also define a macro with the "#define" directive. It is similar to a function in C. We can pass one or more arguments to the macro name and perform the actions in the code segment.

以下代码使用 #define 语句定义了 AREA 宏 −

The following code defines AREA macro using the #define statement −

Example

#include <stdio.h>
#define PI 3.14159
#define AREA(r) (PI*r*r)
int main(){
   int radius = 5;
   float area = AREA(radius);
   printf("Area: %f", area);
   return 0;
}
Area: 78.539749

宏通常执行得比函数快。

Macros are generally faster in execution than the functions.

The main() Function

一个 C 程序是由一个或多个函数的集合。C 程序中有两种类型的函数: library functionsuser-defined functions

A C program is a collection of one or more functions. There are two types of functions in a C program: library functions and user-defined functions.

一个 C 程序中必须至少有一个用户定义的函数,其名称必须为 main() 。main() 函数充当程序的入口点。当程序运行时,编译器会查找 main() 函数。

There must be at least one user-defined function in a C program, whose name must be main(). The main() function serves as the entry point of the program. When the program is run, the compiler looks for the main() function.

main() 函数包含一个或多个语句。默认情况下,每个语句都必须以分号结尾。该语句可能包含变量声明、决策控制或循环构造,或调用库或另一个用户定义的函数。

The main() function contains one or more statements. By default, each statement must end with a semicolon. The statement may include variable declarations, decision control or loop constructs or call to a library or another user-defined function.

在 C 语言中,一个函数必须有一个数据类型。返回值的数据类型必须与函数的数据类型匹配。默认情况下,C 语言中的一个函数属于 int 类型。因此,如果一个函数没有 return statement ,那么它的类型就是 int,您可以在函数定义中省略它,但是编译器会发出警告 −

In C, a function must have a data type. The data type of return value must match with the data type of the function. By default, a function in C is of int type. Hence, if a function doesn’t have a return statement, its type is int, and you may omit it in the function definition, but the compiler issues a warning −

warning: return type defaults to 'int'

Example

以下是一个 main() 函数的典型示例 −

A typical example of main() function is as follows −

#include <stdio.h>
int main() {
   /* my first program in C */
   printf("Hello, World! \n");
   return 0;
}

执行此代码后,您将获得以下输出 −

On executing this code, you will get the following output −

Hello, World!

The Global Declaration Section

此部分包含将用于程序中所有函数的变量声明。用户定义函数的前向声明(稍后在程序中定义)以及用户定义的数据类型也存在于全局部分。

This section consists of declaration of variables to be used across all the functions in a program. Forward declarations of user-defined functions defined later in the program as well as user-defined data types are also present in the global section.

Example of global variable declaration

Example of global variable declaration

int total = 0;
float average = 0.0;

Example of forward declaration of a function

Example of forward declaration of a function

float area(float height, float width);

Subroutines in a C Program

一个 C 程序中可能有多个用户定义的函数。编程最佳实践要求以结构化的方式将编程逻辑分解为独立且可重用的函数。

There may be more than one user-defined functions in a C program. Programming best practices require that the programming logic be broken down to independent and reusable functions in a structured manner.

根据需求,一个 C 程序可能有一个或多个用户定义的函数,这些函数可以从 main() 函数或任何其他用户定义的函数调用。

Depending on the requirements, a C program may have one or more user-defined functions, which may be called from the main() function or any other user-defined function as well.

Comments in a C Program

除了 C 程序的编程元素(如变量、 structuresloops 、函数等)之外,代码还可能在 "/* .. */" 中包含一段文本,它被识别为 comments 。这种注释会被编译器忽略。

Apart from the programming elements of a C program such as variables, structures, loops, functions, etc., the code may have a certain text inside "/* .. */" recognized as comments. Such comments are ignored by the compiler.

在代码中插入注释通常被证明对记录程序以及理解和调试编程逻辑和错误很有帮助。

Inserting comments in the code often proves to be helpful in documenting the program, and in understanding as well as debugging the programming logic and errors.

如果 / 符号没有匹配的 / 符号,编译器会报告错误:"未终止的注释"。

If the / symbol doesn’t have a matching / symbol, the compiler reports an error: "Unterminated comment".

// 之间的一段文本称为 C 风格注释,它被用于插入多行注释。

A text between / and / is called as C-style comment, and is used to insert multi-line comments.

/*
Program to display Hello World
Author: Tutorialspoint
Built with codeBlocks
*/

单行注释以双正斜杠 (//) 开头,以新行结尾。它也可以出现在有效的 C 语句之后。

A single line comment starts with a double forward-slash (//) and ends with a new line. It may appear after a valid C statement also.

int age = 20; // variable to store age

但是,不能在一个以 “//” 开头的行中给出有效的语句。因此,以下语句有错误:

However, a valid statement can’t be given in a line that starts with "//". Hence, the following statement is erroneous:

// Variable to store age. int age=20;

Structure of the C Program

以下代码显示了 C 程序中不同的部分−

The following code shows the different sections in a C program −

/*Headers*/
#include <stdio.h>
#include <math.h>

/*forward declaration*/
float area_of_square(float);

/*main function*/
int main() {
   /* my first program in C */
   float side = 5.50;
   float area = area_of_square(side);
   printf ("Side=%5.2f Area=%5.2f", side, area);
   return 0;
}

/*subroutine*/
float area_of_square(float side){
   float area = pow(side,2);
   return area;
}

执行此代码后,您将获得以下输出 −

On executing this code, you will get the following output −

Side= 5.50 Area=30.25