Cprogramming 简明教程

Special Characters in C

C 语言识别一个包含英语字母(大写和小写(A 到 Z 以及 "a" 到 "z")、数字 0 到 9 和某些称为“特殊字符”且带有特定含义的其他符号的字符集。

The C language identifies a character set that comprises English alphabets – upper and lowercase (A to Z as well as "a" to "z"), digits 0 to 9, and certain other symbols called "special characters" with a certain meaning attached to them.

虽然特殊符号类别中的许多字符被定义为运算符,但某些字符组合也具有特殊的含义。例如, "\n" 称为 newline character. 此类组合称为 escape sequences

While many of the characters in the special symbol category are defined as operators, certain combinations of characters also have a special meaning attached to them. For example, "\n" is known as the newline character. Such combinations are called escape sequences.

在 C 语言中,引号也具有特殊的含义。双引号用于字符串,而字符用单引号括起来。阅读此章节以详细了解在 C 程序中使用的其他特殊字符。

In C language, quotation marks too have a special meaning. Double quotes are used for strings, while characters are enclosed inside single quotes. Read this chapter to learn more about the other special characters used in C programs.

Parentheses ()

括号特别用于对表达式中一个或多个运算数进行分组,并控制语句中的运算顺序。

Parentheses are especially used to group one or more operands in an expression and control the order of operations in a statement.

括号内嵌入了部分表达式,其优先级更高。

A part of the expression embedded inside parentheses has a higher order of precedence.

Example

int a = 2, b = 3, c = 4;
int d = (a + b) * c;

Braces { }

大括号特别用于定义代码块,例如 function 正文和 loops 。它们还用于初始化 arraysstruct variables

Braces are especially used to define blocks of code, such as function bodies and loops. They are also used to initialize arrays and struct variables.

Examples

大括号在函数定义中 -

Braces in function definition −

int add(int a, int b){
   int sum = a + b;
   return sum;
}

大括号在数组初始化中 -

Braces in array initialization −

int arr[5] = {1, 2, 3, 4, 5};

大括号在结构变量中 -

Braces in struct variable −

struct book {
   char title[10];
   double price;
   int pages;
};
struct book b1;
struct book b1 = {"Learn C", 675.50, 325};

Square Brackets [ ]

方括号用于声明数组和使用下标索引访问数组元素。

Square brackets are used to declare arrays and access elements of an array with the subscript index.

Example

例如,要定义一个整数数组并访问其第三个元素,可以使用方括号 -

For example, to define an array of integers and access its third element, you would use square brackets −

int arr[5] = {1, 2, 3, 4, 5};
int third = arr[2];

Asterisk (*)

除了将其用作乘法运算符之外,星号 (*) 符号还用于声明 pointer variabledereference 它以获得目标变量的值。

Apart from its use as a multiplication operator, the asterisk symbol (*) is also used to declare a pointer variable and dereference it to obtain the value of the target variable.

Example

例如,要定义一个指向整数的指针并访问它指向的值,可以使用星号 -

For example, to define a pointer to an integer and access the value it points to, you would use an asterisk −

int num = 10;
int *ptr = #
printf("*d", *ptr);

Ampersand (&)

"&" 符号用作 address-of operator 。它返回变量的地址。

The ampersand (&) symbol is used as the address-of operator. It returns the address of a variable.

Example

例如,要获取一个整型变量的地址,可以使用一个 & -

For example, to get the address of an integer variable, you would use an ampersand −

int num = 10;
int *ptr = #

Comma (,)

逗号用作语句或函数调用之间的分隔符。

The comma is used as a separator between a statement or a function call.

Example

int a = 1, b = 2, c = 3;

Semicolon (;)

作为 C 中的一个主要语法规则,分号指明 C program 中语句的结束。

As a primary syntax rule in C, the semicolon indicates the end of a statement in a C program.

Example

printf("Hello, world!");

Dot (.)

dot symbol (.) 用于访问结构或 union 的成员。

The dot symbol (.) is used to access the members of a structure or a union.

Example

struct book b1 = {"Learn C", 675.50, 325};

printf("Title: %s\n", b1.title);
printf("Price: %lf\n", b1.price);
printf("No of Pages: %d\n", b1.pages);

Arrow (→)

箭头符号 (→) 用于通过指针来访问结构或联合的成员。

The arrow symbol (→) is used to access the members of a structure or a union through a pointer.

Example

struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;

printf("Title: %s\n", strptr->title);
printf("Price: %lf\n", strptr->price);
printf("No of Pages: %d\n", strptr->pages);