Skip to content

Commit afb4802

Browse files
committed
chore: use idoc build website.
1 parent 260d1ba commit afb4802

38 files changed

+585
-142
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
dist
2+
node_modules
3+
package-lock.json
4+
15
# Prerequisites
26
*.d
37

README.md

Lines changed: 41 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,55 @@
1-
Beginning C
2-
---
3-
4-
C 语言是一种功能强大、简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定任务。
5-
6-
目录
1+
C Tutorial
72
===
83

9-
- [创建第一个程序](#创建第一个程序)
10-
- [编译源码](#编译源码)
11-
- [添加注释](#添加注释)
12-
- [使用变量](#使用变量)
13-
- [使用多个变量](#使用多个变量)
14-
- [简单的计算](#简单的计算)
15-
- [计算吃饼干](#计算吃饼干)
16-
- [有浮点值的除法](#有浮点值的除法)
17-
18-
## 创建第一个程序
19-
20-
```c
21-
int main(void)
22-
{
23-
printf("Hello world!");
24-
return 0;
25-
}
26-
```
27-
28-
将上面代码保存为 [`hello.c`](example/hello.c)
29-
30-
## 编译源码
31-
32-
```bash
33-
$ gcc -o hello hello.c
34-
$ ./hello
35-
Hello World
36-
```
37-
38-
## 添加注释
39-
40-
```c
41-
/**
42-
* Written byu Kenny Wong
43-
* Copyright 2019
44-
*/
45-
46-
#include <stdio.h>
47-
48-
// 每个程序总是从 main 这个函数开始执行
49-
int main()
50-
{
51-
printf("Hello world!"); // 这里是单行注释输出日志
52-
return 0; // main 函数 返回了个 0,表示正常终止程序,非 0 表示异常
53-
}
54-
```
55-
56-
## [使用变量](example/using_a_variable.c)
57-
58-
```c
59-
#include <stdio.h>
60-
61-
int main(void)
62-
{
63-
int salary; // 声明一个名为 salary 的变量
64-
salary = 10000; // 将 10000 存储在 salary 中
65-
printf("My salary is %d.\n", salary);
66-
return 0;
67-
}
68-
```
69-
70-
## [使用多个变量](example/using_more_variables.c)
71-
72-
```c
73-
#include <stdio.h>
74-
75-
int main(void)
76-
{
77-
int brothers; // 声明一个名为 brothers 的变量
78-
int brides; // 和一个叫做 brides 的变量
79-
80-
brothers = 7; // 将 7 存储在变量 brothers 中
81-
brides = 7; // 将 7 存储在变量 brides 中
82-
83-
// 输出变量内容
84-
printf("%d brides for %d brothers\n", brides, brothers);
85-
return 0;
86-
}
87-
```
88-
89-
## [简单的计算](example/simple_calculations.c)
90-
91-
```c
92-
#include <stdio.h>
93-
94-
int main(void)
95-
{
96-
int total_pets;
97-
int cats;
98-
int dogs;
99-
int ponies;
100-
int others;
4+
C 语言是一种功能强大、简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定任务。
1015

102-
// 设置每种宠物的数量
103-
cats = 2;
104-
dogs = 1;
105-
ponies = 1;
106-
others = 46;
6+
## 什么是C?
1077

108-
// 计算宠物总数
109-
total_pets = cats + dogs + ponies + others;
8+
C 是 Dennis Ritchie (丹尼斯·里奇) 和 Brian Kernighan (布莱恩·柯林汉) 于 1972 年在贝尔实验室创建的通用编程语言。
1109

111-
printf("我们共有 %d 只宠物!\n", total_pets); // 输出结果
112-
return 0;
113-
}
114-
```
10+
这是一种非常流行的语言,尽管它很古老。
11511

116-
## [计算吃饼干](example/calculations_with_cookies.c)
12+
C 与 UNIX 密切相关,因为它是为编写 UNIX 操作系统而开发的。
11713

118-
```c
119-
#include <stdio.h>
14+
## 为什么要学习 C?
12015

121-
int main(void)
122-
{
123-
int cookies = 5; // 饼干总数
124-
int cookie_calories = 125; // 每卡路里需要的饼干
125-
int total_eaten = 0; // 吃饼干总数
16+
- 它是世界上最流行的编程语言之一
17+
- 如果你懂 C,那么你学习其他流行的编程语言,如 Java、Python、C++、C# 等也没有问题,因为语法相似
18+
- 与其他编程语言(如 Java 和 Python)相比,C 非常快
19+
- C是非常通用的; 它可以用于应用程序和技术
12620

127-
int eaten = 2; // 要吃的数量
128-
cookies = cookies - eaten; // 减去吃掉饼干,获取剩下的饼干
129-
total_eaten = total_eaten + eaten;
130-
printf("\n我吃了 %d 个饼干。 剩下 %d 个饼干。", eaten, cookies);
21+
## C 和 C++ 之间的区别
13122

132-
eaten = 3; // 吃掉饼干的新值,重新赋值
133-
cookies = cookies - eaten; // 减去吃掉饼干,获取剩下的饼干
134-
total_eaten = total_eaten + eaten;
135-
printf("\n我又吃了 %d 个。 现在剩下 %d 个饼干。\n", eaten, cookies);
136-
printf("\n消耗的总能量为 %d 卡路里。\n", total_eaten * cookie_calories);
137-
return 0;
138-
}
139-
```
23+
C++ 是作为 C 的扩展开发的,两种语言的语法几乎相同
24+
C 和 C++ 的主要区别在于 C++ 支持类和对象,而 C 不支持
14025

141-
## [有浮点值的除法](example/division_with_float_values.c)
26+
<!--idoc:ignore:start-->
14227

143-
```c
144-
#include <stdio.h>
28+
## 学习目录
14529

146-
int main(void)
147-
{
148-
float plank_length = 10.0f; // 长度
149-
float piece_count = 4.0f; // 多少块
150-
float piece_length = 0.0f; // 每块的长度
30+
- [C 开始 Get Started](docs/c_getstarted.md)
31+
- [C 语法 Syntax](docs/c_syntax.md)
32+
- [C 输出(打印文本)Output](docs/c_output.md)
33+
- [C 注释 Comments](docs/c_comments.md)
34+
- [C 变量 Variables](docs/c_variables.md)
35+
- [C Data Types](docs/c_data_types.md)
36+
- [C Constants](docs/c_constants.md)
37+
- [C Operators](docs/c_operators.md)
38+
- [C If...Else](docs/c_conditions.md)
39+
- [C Switch](docs/c_switch.md)
40+
- [C While Loop](docs/c_while_loop.md)
41+
- [C For Loop](docs/c_for_loop.md)
42+
- [C Break/Continue](docs/c_break_continue.md)
43+
- [C Arrays](docs/c_arrays.md)
44+
- [C Strings](docs/c_strings.md)
45+
- [C 用户输入 User Input](docs/c_user_input.md)
46+
- [C Memory Address](docs/c_memory_address.md)
47+
- [C Pointers](docs/c_pointers.md)
48+
- [C Functions](docs/c_functions.md)
49+
- [C Function Parameters](docs/c_functions_parameters.md)
50+
- [C Function Declaration](docs/c_functions_decl.md)
51+
- [C Recursion](docs/c_functions_recursion.md)
52+
- [C Math Functions](docs/c_math.md)
53+
- [C Structures](docs/c_structs.md)
15154

152-
piece_length = plank_length / piece_count;
153-
printf("一块 %f 英尺长的木板可以切成 %f 块 %f 英尺长。\n", plank_length, piece_count, piece_length);
154-
return 0;
155-
}
156-
```
55+
<!--idoc:ignore:end-->

assets/editer-01.png

229 KB
Loading

assets/editer-runcode.png

456 KB
Loading

assets/sublime-01.png

142 KB
Loading

assets/sublime-02.png

252 KB
Loading

docs/c_arrays.md

Whitespace-only changes.

docs/c_break_continue.md

Whitespace-only changes.

docs/c_comments.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
C 注释 Comments
2+
===
3+
4+
注释可用于解释代码,并使其更具可读性。它还可用于在测试替代代码时阻止执行。
5+
6+
注释可以是单行或多行的。
7+
8+
## 单行注释
9+
10+
单行注释以两个正斜杠 (`//`) 开头。
11+
12+
`//` 和行尾之间的任何文本都会被编译器忽略(不会被执行)。
13+
14+
此示例在一行代码之前使用单行注释:
15+
16+
```c
17+
// 这是一个注释
18+
printf("Hello World!");
19+
```
20+
21+
此示例在代码行末尾使用单行注释:
22+
23+
```c
24+
printf("Hello World!"); // 这是一个注释
25+
```
26+
27+
## 多行注释
28+
29+
多行注释以 `/*` 开头,以 `*/` 结尾。
30+
31+
`/*``*/` 之间的任何文本都将被编译器忽略:
32+
33+
34+
```c
35+
/* 下面的代码将打印出 Hello World!
36+
到屏幕上,真是太棒了 */
37+
printf("Hello World!");
38+
```
39+
40+
## 单行注释还是多行注释?
41+
42+
您要使用哪个取决于您。 通常,我们使用 `//` 表示简短的注释,使用 `/*` `*/` 表示较长的注释。
43+
44+
很高兴知道:在 C99 版本(1999 年发布)之前,您只能在 `C` 中使用多行注释。

docs/c_compiler.md

Whitespace-only changes.

docs/c_conditions.md

Whitespace-only changes.

docs/c_constants.md

Whitespace-only changes.

docs/c_data_types.md

Whitespace-only changes.

docs/c_examples.md

Whitespace-only changes.

docs/c_exercises.md

Whitespace-only changes.

docs/c_for_loop.md

Whitespace-only changes.

docs/c_functions.md

Whitespace-only changes.

docs/c_functions_decl.md

Whitespace-only changes.

docs/c_functions_parameters.md

Whitespace-only changes.

docs/c_functions_recursion.md

Whitespace-only changes.

docs/c_getstarted.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
开始使用 C
2+
===
3+
4+
要开始使用 C,您需要做两件事:
5+
6+
- 用于编写 C 代码的文本编辑器,如记事本
7+
- 编译器,如 GCC,将 C 代码翻译成计算机可以理解的语言
8+
9+
有许多文本编辑器和编译器可供选择。 我们介绍几种编辑器和编译环境(见下文)。
10+
11+
注意 ⚠️ :Linux 和 Mac 系统可以直接安装 GCC,Windows 系统可以安装 MinGW。
12+
13+
## 编辑器
14+
15+
### Visual Studio Code
16+
17+
[![VSCode 下载安装地址](../assets/editer-01.png)](https://code.visualstudio.com)
18+
19+
VSCode 下载安装地址:https://code.visualstudio.com
20+
21+
下载安装好 [VSCode](https://code.visualstudio.com) 后,我们需要安装下面两个插件更方便的支持 C 语言环境。
22+
23+
- [C/C++ Extension Pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools-extension-pack)
24+
- [Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner)
25+
26+
### Sublime Text 3
27+
28+
[![Sublime Text 下载安装地址](../assets/sublime-01.png)](https://www.sublimetext.com)
29+
30+
Sublime Text 下载安装地址:https://www.sublimetext.com
31+
32+
## C 快速入门
33+
34+
如果您不理解下面的代码,请不要担心 —— 我们将在后面的章节中详细讨论。 现在,专注于如何运行代码。
35+
36+
### 创建第一个程序
37+
38+
让我们创建我们的第一个 C 文件。
39+
40+
打开编辑器选择菜单 `File` > `New File` 创建一个空的文本文件。
41+
42+
编写以下 C 代码并将文件另存为 `hello.c``File` > `Save As`):
43+
44+
```c
45+
#include <stdio.h>
46+
47+
int main() {
48+
printf("Hello World!");
49+
return 0;
50+
}
51+
```
52+
53+
### 编辑器编译运行
54+
55+
[Visual Studio Code](https://code.visualstudio.com) 中,它应该如下所示:
56+
57+
![](../assets/editer-runcode.png)
58+
59+
然后,点击 `Run Code` 以运行(执行)程序。 结果看起来像这样:
60+
61+
```bash
62+
[Running] cd "/Users/c-tutorial/example/" && gcc hello.c -o hello && "/Users/c-tutorial/example/"hello
63+
Hello world!
64+
[Done] exited with code=0 in 4.016 seconds
65+
```
66+
67+
在 Sublime Text 3 中,运行比较简单,打开 `hello.c` 文件,`Tool` > `Build` 选择 `C Single File - Run`,它应该如下所示:
68+
69+
[![Sublime Text 下载安装地址](../assets/sublime-02.png)](https://www.sublimetext.com)
70+
71+
### 命令行编译运行
72+
73+
通过命令行进入 `hello.c` 所在目录
74+
75+
```bash
76+
cd /Users/c-tutorial/example
77+
```
78+
79+
使用 `gcc` 编译 `hello.c` 文件,
80+
81+
```bash
82+
$ gcc -o hello hello.c
83+
```
84+
85+
运行成功之后会在当前目录下看到 hello 文件,直接在命令后输入 `./hello` 命令,您将得到如下输出结果:
86+
87+
```bash
88+
Hello World
89+
```
90+
91+
### Docker 运行环境
92+
93+
可以简单用 Docker 来安装编译环境,这样在 Windows、MacOS、Linux 下都可以拥有完全相同的编译环境。
94+
95+
镜像基于 Alpine Linux 镜像,只有 5MB 镜像,并且包含 C/C++ 编译器(gcc/g++ 包)
96+
97+
1) 安装 Docker
98+
2) 使用 docker pull 命令
99+
```bash
100+
docker pull frolvlad/alpine-gxx
101+
```
102+
3) 创建 Dockerfile 文件
103+
```docker
104+
FROM alpine:3.14
105+
RUN apk add --no-cache gcc musl-dev
106+
RUN apk add --no-cache g++
107+
```
108+
4) 生成本地镜像
109+
```bash
110+
docker build -t myalpine .
111+
```
112+
5) 运行映像,把当前路径($PWD)映射至容器的 /test 目录,用 gcc 编译程序,exit返回:
113+
```shell
114+
docker run -it -v$PWD:/test myalpine
115+
root@b1a38bd7107a:/# cd test
116+
root@b1a38bd7107a:/test# gcc -o hello hello.c
117+
Hello World
118+
root@b1a38bd7107a:/test# exit
119+
exit
120+
```

docs/c_math.md

Whitespace-only changes.

docs/c_memory_address.md

Whitespace-only changes.

docs/c_operators.md

Whitespace-only changes.

0 commit comments

Comments
 (0)