提交 38e5ec82 编写于 作者: W wizardforcel

2020-06-29 11:09:02

上级 d15ea00a
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
要理解此示例,您应该了解以下 [C 编程](/c-programming "C tutorial")主题: 要理解此示例,您应该了解以下 [C 编程](/c-programming "C tutorial")主题:
* [C 预处理程序和宏](/c-programming/c-preprocessor-macros) * [C 预处理和宏](/c-programming/c-preprocessor-macros)
* [C 文件处理](/c-programming/c-file-input-output) * [C 文件处理](/c-programming/c-file-input-output)
* * * * * *
......
...@@ -25,7 +25,8 @@ C 接受小写字母和大写字母作为变量和函数。 ...@@ -25,7 +25,8 @@ C 接受小写字母和大写字母作为变量和函数。
### 特殊的角色 ### 特殊的角色
<caption>Special Characters in C Programming</caption> Special Characters in C Programming
| , | < | > | . | _ | | , | < | > | . | _ |
| ( | ) | ; | $ | : | | ( | ) | ; | $ | : |
| % | [ | ] | # | ? | | % | [ | ] | # | ? |
...@@ -51,7 +52,8 @@ int money; ...@@ -51,7 +52,8 @@ int money;
由于 C 是区分大小写的语言,因此所有关键字都必须使用小写字母。 这是 ANSI C 中允许的所有关键字的列表。 由于 C 是区分大小写的语言,因此所有关键字都必须使用小写字母。 这是 ANSI C 中允许的所有关键字的列表。
<caption>C Keywords</caption> C Keywords
| `auto` | `double` | `int` | `struct` | | `auto` | `double` | `int` | `struct` |
| `break` | `else` | `long` | `switch` | | `break` | `else` | `long` | `switch` |
| `case` | `enum` | `register` | ``typedef`` | | `case` | `enum` | `register` | ``typedef`` |
......
...@@ -102,7 +102,8 @@ Hexadecimal: 0x7f, 0x2a, 0x521 etc ...@@ -102,7 +102,8 @@ Hexadecimal: 0x7f, 0x2a, 0x521 etc
为了使用这些字符,使用了转义序列。 为了使用这些字符,使用了转义序列。
<caption>Escape Sequences</caption> Escape Sequences
| 转义序列 | 字符 | | 转义序列 | 字符 |
| --- | --- | | --- | --- |
| `\b` | 退格键 | | `\b` | 退格键 |
...@@ -156,4 +157,4 @@ PI = 2.9; //Error ...@@ -156,4 +157,4 @@ PI = 2.9; //Error
* * * * * *
您也可以使用`#define`预处理程序指令定义常量。 我们将在 [C 宏](/c-programming/c-preprocessor-macros "C Macros")教程中对其进行了解。 您也可以使用`#define`预处理器指令定义常量。 我们将在 [C 宏](/c-programming/c-preprocessor-macros "C Macros")教程中对其进行了解。
\ No newline at end of file \ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
> 原文: [https://www.programiz.com/c-programming/c-structure-function](https://www.programiz.com/c-programming/c-structure-function) > 原文: [https://www.programiz.com/c-programming/c-structure-function](https://www.programiz.com/c-programming/c-structure-function)
#### 在本教程中,您将学习将结构变量作为参数传递给函数。 您将学习借助示例从函数返回 struct 的方法。 #### 在本教程中,您将学习将结构变量作为参数传递给函数。 您将学习借助示例从函数返回`struct`的方法。
与内置类型的变量类似,您也可以将结构变量传递给函数。 与内置类型的变量类似,您也可以将结构变量传递给函数。
......
# C 联 # C 联
> 原文: [https://www.programiz.com/c-programming/c-unions](https://www.programiz.com/c-programming/c-unions) > 原文: [https://www.programiz.com/c-programming/c-unions](https://www.programiz.com/c-programming/c-unions)
......
...@@ -20,5 +20,5 @@ ...@@ -20,5 +20,5 @@
| [加上两个距离(以英寸英尺为单位)](/c-programming/examples/inch-feet-structure) | | [加上两个距离(以英寸英尺为单位)](/c-programming/examples/inch-feet-structure) |
| [通过将结构传递给函数](/c-programming/examples/complex-number-add)来添加两个复数 | | [通过将结构传递给函数](/c-programming/examples/complex-number-add)来添加两个复数 |
| [计算两个时间段之间的时差](/c-programming/examples/time-structure) | | [计算两个时间段之间的时差](/c-programming/examples/time-structure) |
| [使用结构](/c-programming/examples/information-structure-array)存储 10 名学生的信息 | | [使用结构存储 10 名学生的信息](/c-programming/examples/information-structure-array) |
| [使用结构](/c-programming/examples/structure-dynamic-memory-allocation)存储 n 名学生的信息 | | [使用结构存储 n 名学生的信息](/c-programming/examples/structure-dynamic-memory-allocation) |
\ No newline at end of file \ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
> 原文: [https://www.programiz.com/c-programming/c-file-input-output](https://www.programiz.com/c-programming/c-file-input-output) > 原文: [https://www.programiz.com/c-programming/c-file-input-output](https://www.programiz.com/c-programming/c-file-input-output)
#### 在本教程中,您将学习 C 语言中的文件处理。您将学习在 f 的帮助下使用 fprintf(),fscanf(),fread(),fwrite(),fseek()等在 C 中处理标准 I/O。 例子。 #### 在本教程中,您将学习 C 语言中的文件处理。您将学习在示例的帮助下使用`fprintf()`,`fscanf()`,`fread()`,`fwrite()`,`fseek()`等在 C 中处理标准 I/O。
文件是计算机存储设备中用于存储数据的容器。 文件是计算机存储设备中用于存储数据的容器。
...@@ -87,10 +87,11 @@ fopen("E:\\cprogram\\oldprogram.bin","rb"); ...@@ -87,10 +87,11 @@ fopen("E:\\cprogram\\oldprogram.bin","rb");
* 现在,我们假设第二个二进制文件`oldprogram.bin`存在于位置`E:\cprogram`中。 第二个功能打开现有文件,以二进制模式**'rb'**读取。 * 现在,我们假设第二个二进制文件`oldprogram.bin`存在于位置`E:\cprogram`中。 第二个功能打开现有文件,以二进制模式**'rb'**读取。
读取模式仅允许您读取文件,而不能写入文件。 读取模式仅允许您读取文件,而不能写入文件。
<caption>Opening Modes in Standard I/O</caption> Opening Modes in Standard I/O
| 模式 | 模式的含义 | 文件不存在期间 | | 模式 | 模式的含义 | 文件不存在期间 |
| --- | --- | --- | | --- | --- | --- |
| `r` | 开放供阅读。 | 如果文件不存在,则`fopen()`返回 NULL。 | | `r` | 开放供阅读。 | 如果文件不存在,则`fopen()`返回`NULL`。 |
| `rb` | 打开以二进制模式读取。 | If the file does not exist, `fopen()` returns NULL. | | `rb` | 打开以二进制模式读取。 | If the file does not exist, `fopen()` returns NULL. |
| `w` | 开放写作。 | 如果文件存在,其内容将被覆盖。 | `w` | 开放写作。 | 如果文件存在,其内容将被覆盖。
如果文件不存在,将创建它。 | 如果文件不存在,将创建它。 |
...@@ -129,7 +130,7 @@ fclose(fptr); ...@@ -129,7 +130,7 @@ fclose(fptr);
为了读取和写入文本文件,我们使用功能`fprintf()``fscanf().` 为了读取和写入文本文件,我们使用功能`fprintf()``fscanf().`
它们只是`printf()``scanf()`的文件版本。 唯一的区别是`fprint()``fscanf()`需要指向结构 FILE 的指针。 它们只是`printf()``scanf()`的文件版本。 唯一的区别是`fprint()``fscanf()`需要指向结构`FILE`的指针。
* * * * * *
...@@ -226,7 +227,7 @@ fwrite(addressData, sizeData, numbersData, pointerToFile); ...@@ -226,7 +227,7 @@ fwrite(addressData, sizeData, numbersData, pointerToFile);
* * * * * *
### 示例 3:使用 fwrite()写入二进制文件 ### 示例 3:使用`fwrite()`写入二进制文件
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -265,9 +266,9 @@ int main() ...@@ -265,9 +266,9 @@ int main()
在此程序中,我们在 C 驱动器中创建一个新文件`program.bin` 在此程序中,我们在 C 驱动器中创建一个新文件`program.bin`
我们用三个数字- `n1,n2 和 n3`声明结构`threeNum`,并在主函数中将其定义为 num 我们用三个数字 - `n1``n2``n3`声明结构`threeNum`,并在主函数中将其定义为`num`
现在,在 for 循环中,我们使用`fwrite()`将值存储到文件中。 现在,在`for`循环中,我们使用`fwrite()`将值存储到文件中。
第一个参数采用`num`的地址,第二个参数采用结构`threeNum`的大小。 第一个参数采用`num`的地址,第二个参数采用结构`threeNum`的大小。
...@@ -287,7 +288,7 @@ fread(addressData, sizeData, numbersData, pointerToFile); ...@@ -287,7 +288,7 @@ fread(addressData, sizeData, numbersData, pointerToFile);
* * * * * *
### 示例 4:使用 fread()从二进制文件读取 ### 示例 4:使用`fread()`从二进制文件读取
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -330,7 +331,7 @@ int main() ...@@ -330,7 +331,7 @@ int main()
* * * * * *
## 使用 fseek()获取数据 ## 使用`fseek()`获取数据
如果文件中有很多记录,并且需要访问特定位置的记录,则需要遍历所有记录才能获取该记录。 如果文件中有很多记录,并且需要访问特定位置的记录,则需要遍历所有记录才能获取该记录。
...@@ -340,7 +341,7 @@ int main() ...@@ -340,7 +341,7 @@ int main()
* * * * * *
### fseek()的语法 ### `fseek()`的语法
```c ```c
fseek(FILE * stream, long int offset, int whence); fseek(FILE * stream, long int offset, int whence);
...@@ -348,7 +349,8 @@ fseek(FILE * stream, long int offset, int whence); ...@@ -348,7 +349,8 @@ fseek(FILE * stream, long int offset, int whence);
第一个参数流是指向文件的指针。 第二个参数是要查找的记录的位置,第三个参数指定偏移量开始的位置。 第一个参数流是指向文件的指针。 第二个参数是要查找的记录的位置,第三个参数指定偏移量开始的位置。
<caption>Different whence in fseek()</caption> Different whence in fseek()
| 何处 | 含义 | | 何处 | 含义 |
| --- | --- | | --- | --- |
| `SEEK_SET` | 从文件开头开始偏移。 | | `SEEK_SET` | 从文件开头开始偏移。 |
...@@ -357,7 +359,7 @@ fseek(FILE * stream, long int offset, int whence); ...@@ -357,7 +359,7 @@ fseek(FILE * stream, long int offset, int whence);
* * * * * *
### 示例 5:fseek() ### 示例 5:`fseek()`
```c ```c
#include <stdio.h> #include <stdio.h>
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
## C 文件示例 ## C 文件示例
**1\. C 程序读取 n 个学生的姓名和分数并将它们存储在文件中。** **1\. C 程序读取 n 个学生的姓名和分数并将它们存储在文件中。**
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -51,7 +51,7 @@ int main() ...@@ -51,7 +51,7 @@ int main()
} }
``` ```
**2\. C 程序读取 n 个学生的姓名和标志并将它们存储在文件中。 如果文件先前已退出,则将信息添加到文件中。** **2\. C 程序读取 n 个学生的姓名和标志并将它们存储在文件中。 如果文件先前已退出,则将信息添加到文件中。**
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -87,7 +87,7 @@ int main() ...@@ -87,7 +87,7 @@ int main()
} }
``` ```
**3\. C 程序使用 fwrite()将结构数组的所有成员写入文件。 从文件中读取阵列并显示在屏幕上。** **3\. C 程序:使用`fwrite()`将结构数组的所有成员写入文件。 从文件中读取阵列并显示在屏幕上。**
```c ```c
#include <stdio.h> #include <stdio.h>
......
...@@ -120,7 +120,7 @@ enum designFlags { ...@@ -120,7 +120,7 @@ enum designFlags {
} button; } button;
``` ```
假设您正在设计 Windows 应用程序的按钮。 您可以将`,[italics],`,粗体`和`下划线`标记设置为使用文本。` 假设您正在设计 Windows 应用程序的按钮。 您可以将文本设置为使用`ITALICS``BOLD``UNDERLINE`标记。
在上述伪代码中,所有积分常数均为 2 的幂是有原因的。 在上述伪代码中,所有积分常数均为 2 的幂是有原因的。
...@@ -133,7 +133,7 @@ UNDERLINE = 00000100 ...@@ -133,7 +133,7 @@ UNDERLINE = 00000100
``` ```
由于积分常数是 2 的幂,因此可以使用[按位或|一次组合两个或多个标志而不会重叠。 运算符](/c-programming/bitwise-operators#or "Bitwise OR operator in C programming")。 这使您可以一次选择两个或多个标志。 例如, 由于积分常数是 2 的幂,因此可以使用[按位或`|`运算符一次组合两个或多个标志而不会重叠](/c-programming/bitwise-operators#or "Bitwise OR operator in C programming")。 这使您可以一次选择两个或多个标志。 例如,
```c ```c
#include <stdio.h> #include <stdio.h>
......
# C 预处理程序和宏 # C 预处理和宏
> 原文: [https://www.programiz.com/c-programming/c-preprocessor-macros](https://www.programiz.com/c-programming/c-preprocessor-macros) > 原文: [https://www.programiz.com/c-programming/c-preprocessor-macros](https://www.programiz.com/c-programming/c-preprocessor-macros)
#### 在本教程中,将向您介绍 c 预处理器,并在示例的帮助下学习使用#include,#define 和条件编译。 #### 在本教程中,将向您介绍 c 预处理器,并在示例的帮助下学习使用`#include`,`#define`和条件编译。
![Working of a preprocessor in C programming](img/765b3e84f88056116bb8b07ab90d19d6.png) ![Working of a preprocessor in C programming](img/765b3e84f88056116bb8b07ab90d19d6.png)
C 预处理程序是一个宏预处理程序(允许您定义宏),可以在编译程序之前对其进行转换。 这些转换可以包括头文件,宏扩展等。 C 预处理器是一个宏预处理器(允许您定义宏),可以在编译程序之前对其进行转换。 这些转换可以包括头文件,宏扩展等。
所有预处理指令均以`#`符号开头。 例如, 所有预处理指令均以`#`符号开头。 例如,
...@@ -14,7 +14,7 @@ C 预处理程序是一个宏预处理程序(允许您定义宏),可以在 ...@@ -14,7 +14,7 @@ C 预处理程序是一个宏预处理程序(允许您定义宏),可以在
#define PI 3.14 #define PI 3.14
``` ```
C 预处理程序的一些常见用法是: C 预处理的一些常见用法是:
* * * * * *
...@@ -26,11 +26,11 @@ C 预处理程序的一些常见用法是: ...@@ -26,11 +26,11 @@ C 预处理程序的一些常见用法是:
#include <stdio.h> #include <stdio.h>
``` ```
在此,`stdio.h`是头文件。`#include`预处理程序指令将上述行替换为`stdio.h`头文件的内容。 在此,`stdio.h`是头文件。`#include`预处理指令将上述行替换为`stdio.h`头文件的内容。
这就是为什么在使用`scanf()``printf()`之类的功能之前需要使用`#include <stdio.h>`的原因。 这就是为什么在使用`scanf()``printf()`之类的功能之前需要使用`#include <stdio.h>`的原因。
您还可以创建自己的包含函数声明的头文件,并使用此预处理程序指令将其包含在程序中。 您还可以创建自己的包含函数声明的头文件,并使用此预处理指令将其包含在程序中。
```c ```c
#include "my_header.h" #include "my_header.h"
...@@ -40,9 +40,9 @@ C 预处理程序的一些常见用法是: ...@@ -40,9 +40,9 @@ C 预处理程序的一些常见用法是:
* * * * * *
## 使用#define 的宏 ## 使用`#define`的宏
宏是被命名的代码片段。 您可以使用`#define`预处理程序指令在 C 中定义宏。 宏是被命名的代码片段。 您可以使用`#define`预处理指令在 C 中定义宏。
这是一个例子。 这是一个例子。
...@@ -54,7 +54,7 @@ C 预处理程序的一些常见用法是: ...@@ -54,7 +54,7 @@ C 预处理程序的一些常见用法是:
* * * * * *
### 示例 1:#define 预处理程序 ### 示例 1:`#define`预处理器
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -94,7 +94,7 @@ circleArea(5) expands to (3.1415*5*5) ...@@ -94,7 +94,7 @@ circleArea(5) expands to (3.1415*5*5)
* * * * * *
### 示例 2:使用#define 预处理程序 ### 示例 2:使用`#define`预处理器
```c ```c
#include <stdio.h> #include <stdio.h>
...@@ -113,7 +113,7 @@ int main() { ...@@ -113,7 +113,7 @@ int main() {
} }
``` ```
访问此页面以了解有关[宏和#define 预处理器](https://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros "C Macros")的更多信息。 访问此页面以了解有关[宏和`#define`预处理器](https://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros "C Macros")的更多信息。
* * * * * *
...@@ -141,7 +141,7 @@ int main() { ...@@ -141,7 +141,7 @@ int main() {
* * * * * *
### #ifdef 指令 ### `#ifdef`指令
```c ```c
#ifdef MACRO #ifdef MACRO
...@@ -154,7 +154,7 @@ int main() { ...@@ -154,7 +154,7 @@ int main() {
* * * * * *
### #if,#elif 和#else 指令 ### `#if`,`#elif`和`#else`指令
```c ```c
#if expression #if expression
...@@ -211,7 +211,7 @@ int main() { ...@@ -211,7 +211,7 @@ int main() {
这是 C 编程中的一些预定义宏。 这是 C 编程中的一些预定义宏。
| 巨集 | 值 | | | 值 |
| --- | --- | | --- | --- |
| `__DATE__` | 包含当前日期的字符串 | | `__DATE__` | 包含当前日期的字符串 |
| `__FILE__` | 包含文件名的字符串 | | `__FILE__` | 包含文件名的字符串 |
...@@ -221,7 +221,7 @@ int main() { ...@@ -221,7 +221,7 @@ int main() {
* * * * * *
### 示例 3:使用 __TIME__ 获取当前时间 ### 示例 3:使用`__TIME__`获取当前时间
以下程序使用`__TIME__`宏输出当前时间。 以下程序使用`__TIME__`宏输出当前时间。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册