Cprogramming 简明教程
C - Basic Syntax
在 C programming 中,“语法”一词指的是为程序员制定的规则集,用于编写特定应用程序的源代码。虽然针对 C 中的每个关键字都建议使用特定的语法,但在开发 C 程序时需要遵循某些一般规则。
In C programming, the term "syntax" refers to the set of rules laid down for the programmer to write the source code of a certain application. While there is a specific syntax recommended for each of the keywords in C, certain general rules need to be followed while developing a program in C.
C 程序的典型源代码具有以下元素 −
A typical source code of a C program has the following elements −
/*Hello World program*/ // Comments
#include <stdio.h> // Header File
int a=10; // Global declarations
// The main function
int main() {
char message[] = "Hello World"; // Local variable
printf("%s", message);
return 0;
}
Tokens in C
C 程序包含各种 tokens ,并且令牌要么是关键字、标识符、常量、字符串文字或符号。例如,以下 C 语句包含五个令牌 −
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following C statement consists of five tokens −
printf("Hello, World! \n");
各个令牌为 −
The individual tokens are −
printf
(
"Hello, World! \n"
);
C compiler 标识令牌是否为关键字、标识符、注释、 literal 、运算符、任何其他已识别的特殊符号。此操作由编译过程的第一阶段中的标记生成器完成。
The C compiler identifies whether the token is a keyword, identifier, comment, a literal, an operator, any of the other recognized special symbols or not. This exercise is done by the tokenizer in the first stage of the compilation process.
Identifiers in C
C identifier 是一个用于标识变量、函数或任何其他用户定义项的名称。标识符以字母“A”到“Z”、“a”到“z”或下划线“_”开头,后跟零个或多个字母、下划线和数字 (0 到 9)。
A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to 9).
C 规定了形成变量名、函数名或其他编程元素的某些规则。它们不是关键字。标识符必须以字母或下划线字符开头,并且除了字母、数字和下划线之外不得有任何其他字符。
C prescribes certain rules to form the names of variables, functions or other programming elements. They are not keywords. An identifier must start with an alphabet or underscore character, and must not have any other character apart from alphabets, digits and underscore.
C 不允许标识符中出现标点符号,如 @、$ 和 %。C 是一种 case−sensitive 编程语言。因此,在 C 中“Manpower”和“manpower”是两个不同的标识符。以下是一些可接受标识符的示例 −
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case−sensitive programming language. Thus, Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
Keywords in C
C 语言中最重要部分是它的关键字。 Keywords 是保留字,其预定义含义具有规定的语法以用于使用。在 ANSI C 中,所有关键字都使用小写字母。程序员需要选择正确的关键字来构建手头问题的解决方案。学习编程基本上就是要学会正确使用关键字。
The most important part of C language is its keywords. Keywords are the reserved words having a predefined meaning with prescribed syntax for usage. In ANSI C, all keywords have lowercase alphabets. The programmer needs to choose the correct keywords to construct the solution of the problem at hand. To learn programming is basically to learn to correctly use the keywords.
以下列表显示了 C 中的保留字。这些保留字不能用作 constants 或变量或任何其他标识符名称。
The following list shows the reserved words in C. These reserved words may not be used as constants or variables or any other identifier names.
auto |
else |
long |
switch |
break |
enum |
register |
typedef |
case |
extern |
return |
union |
char |
float |
short |
unsigned |
const |
for |
signed |
void |
continue |
goto |
sizeof |
volatile |
default |
if |
static |
while |
do |
int |
struct |
_Packed |
double |
除了本章中解释的基本语法外,C 中的每个关键字都有一个明确定义的语法。将在后续章节中解释每个关键字的用法。
Each keyword in C has a well−defined syntax in addition to the basic syntax explained in this chapter. The usage of each keyword will be explained in the subsequent chapters.
Semicolons in C
在 C 程序中,分号是语句终止符。也就是说,每个单独的语句都必须以分号结尾。它表示一个逻辑实体的结束。
In a C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
以下有两种不同的陈述 -
Given below are two different statements −
printf("Hello, World! \n");
return 0;
由于分号“;”只是 C 语句的分隔符,因此在 C 程序的一行物理行中可能有多条语句。同样,一个语句可能跨源代码中的多行。
Since the semicolon ";" is only the delimiter symbol for a C statement, there may be more than one statements in a one physical line in a C program. Similarly, a single statement may span over more than one lines in the source code.
C 语言中的以下行完全有效。一行中有多条语句 -
The following line is perfectly valid in C. In one line, there are multiple statements −
int a=10; if (a>=50) printf("pass"); else printf("fail");
即使一条语句溢出多行,以下代码也是有效的 -
The following code is also valid even if a statement spills over multiple lines −
if
(a>=50)
printf("pass");
else printf("fail");
Comments in C
Comments 就像 C 程序中的帮助文本,并且它们被编译器忽略。它们以 /* 开始,并以 */ 结尾,如下所示 -
Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminate with the characters */ as shown below −
/* my first program in C */
您不可在注释内有注释,并且它们不发生在字符串或字符文本中。
You cannot have comments within comments and they do not occur within a string or character literals.
Source Code
C 程序是一个文本文件,包含一系列语句。文件必须以 .c 为扩展名。C 编译器只识别 .c 文件进行编译过程。关键字及 C 语言的其他标识符只使用英语字母,尽管字符串文本可以包含任何 Unicode 字符。
The C program is a text file, containing a series of statements. The file must have a .c as its extension. The C compiler identifies only the .c file for compilation process. Only the English alphabets are used in the keywords and other identifiers of C language, although the string literals may contain any Unicode characters.
The main() Function
每个 C 程序必须有一个(且仅有一个) main() function ,编译器从中开始执行代码。然而,不必在 .c 文件中把 main() 函数放在代码的开头。C 程序中可以有任何数量的函数。如果一个函数在其定义之前调用任何其他函数,应该有其前向声明。
Every C program must have one (and only one) main() function, from where the compiler starts executing the code. However, it is not necessary that the main() function should be in the beginning of the code in the .c file. There can be any number of functions in a C program. If a function calls any other function before its definition, there should be its forward declaration.
Header Files
除了关键字之外,C 程序还常常需要从 header files 的库中调用预定义函数。所需的头部文件用 #include preprocessor directive 导入。所有 #include 语句必须在源代码的开头。
In addition to the keywords, a C program often needs to call predefined functions from the library of header files. The required header files are imported with the #include preprocessor directive. All the #include statements must be in the beginning of the source code.
Variable Declaration
C 是一种静态类型语言。它要求在使用前声明程序中出现的所有 variables 。变量可以在全局声明,即在任何 function 外面,或者在函数的 scope 内本地声明。变量只能存储其声明类型的某个值。这是 C 语言重要的规则之一。
C is a statically typed language. It requires, all the variables appearing in a program to be declared before using. Variables can be declared globally i.e. outside any function, or locally within the scope of a function. The variable can store a value only of its declared type. This is one of the important rules of C.
Statements in a C Program
语句是程序的基本构建块。默认情况下,main() 函数中的语句按从上到下的顺序执行。顺序由 conditionals 或 looping constructs 控制。作为一个基本的语法规则,每条语句都必须在结尾处带分号 (;)。
Statements are the basic building blocks of the program. Statements in the main() function are executed in a top to bottom order by default. The sequence is controlled by conditionals or looping constructs. As a basic syntax rule, each statement must have semicolon (;) at the end.
Whitespaces in a C Program
编译源代码时,编译器会忽略空白字符。空白字符是 C 中用来描述空格、制表符、换行字符和注释的术语。虽然可以使用它们提高代码的可读性,但它们对编译器的意义很小(除非它们是字符串文本的一部分,出现在双引号符号内)。仅包含空白字符(可能带有注释)的行称为空白行, C compiler 会完全忽略它。
While compiling the source code, the compiler ignores the whitespaces. Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. While one can use them for better readability of the code, they have little significance for the compiler (unless they are a part of a string literal, appearing inside the double quote symbols). A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.
空白字符将语句的一部分与另一部分分隔开,并允许编译器识别语句中一个元素的结尾(比如 int)以及下一个元素的开始。因此,在以下语句中 -
Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the following statement −
int age;
int 和 age 之间必须至少有一个空白字符(通常是一个空格),以便编译器能够区分它们。另一方面,在以下语句中 -
There must be at least one whitespace character (usually a space) between int and age for the compiler to be able to distinguish them. On the other hand, in the following statement −
fruit = apples + oranges; // get the total fruit
“fruit”和“=”之间或“=”和“apples”之间不需要空白字符,尽管如果您愿意提高可读性,您可以自由地包含一些空白字符。
No whitespace characters are necessary between "fruit" and "=", or between "=" and "apples", although you are free to include some if you wish to increase readability.
Compound Statements in C
通常,您需要将一个连贯的语句块定义为一个单一的编程逻辑单元。例如,您可能希望当某个逻辑表达式为 true 时执行多于一条语句,或者在循环块中执行多条语句。同样,用户定义的函数可能有多条语句。在这种情况下,将语句组合在一起以形成一个复合语句。C 使用大括号进行这种组合。
Often, you need to define a cohesive block of statements as a single unit of programming logic. For example, you may want more than one statements to be executed if a certain logical expression is true, or multiple statements in a looping block. Similarly, a user-defined function may have more than one statements. In such cases, statements are grouped together to form a compound statement. C uses curly brackets for such grouping.
{
Statement1;
Statement2;
. . .
. . .
}
在以下代码中,if 和 else 部分各有一个语句块。
In the following code, the if and else parts have a block each of statements.
int marks = 40;
if (marks<50) {
printf("Result: Fail\n");
printf ("Better Luck next time");
}
else {
printf("Result: Pass\n");
printf("Congratulations");
}
大括号也用于函数定义中:
Curly brackets are also used in the function definition:
float area_of_square(float side) {
float area = pow(side,2);
return area;
}
Defining a custom type with struct, union or enum also requires clubbing more than one statements together with curly brackets.
struct student {
char name[20];
int marks, age;
};
花括号还声明了一个数组如下 −
The curly brackets also declare an array as follows −
int marks[ ]={50,56,76,67,43};