Harvard CS50 学习笔记(二)
目录
摘要
Harvard CS50 学习笔记(二)。
2 Array
2.1 Lecture
2.1 Make
2.2 Clang
-
C 语言
的编译器前端。 -
clang <name>.c
源码文件,对源码文件进行编译,并生成可执行文件a.out (默认名称)。 -
如果需要指定名称,使用
clang -o <name> <name>.c
。 -
如果源码包含了非标准库的头文件,使用clang 编译时需要指定关联,否则报错。
clang -o <name> <name>.c -l<name of .h file>
2.3 Compiling (Four Steps)
- Preprocessing
- Compiling
- Assembling
- Linking
2.4 Preprocessing

- 源码最上面的
#
,实际为preprocessor directive
,即预处理指令。 - 含有
#
的代码会被特殊处理。 - 用
#
引入的头文件,类似于找到头文件的代码,复制粘贴到源码最上方。(其实只是函数的声明,没有实现)

2.5 From source code to machine code
- 把
C语言
,编译为汇编语言(Assembling Language
)。 - 左边的东西叫
汇编指令
(Assembling Instruction
)。

2.6 Assembling
- 把汇编语言变成01字节码。

2.7 Linking
- 把用到的不同程序,例如引入的头文件的源码,与当前程序进行链接。
- 头文件只有声明,这一步是找到其实现,进行链接。
2.8 Debugging
2.9 buggy.c
2.10 printf
2.11 debug50
2.12 Rubber Duck Debugging
- 竟然是《这就是软件工程师》里面,陈智峰提到的小黄鸭调试法。
2.13 get_negative_int
2.14 Step in
2.15 Types and their sizes

2.16 Memory
2.17 scores.c
2.18 Arrays
- 数组:储存同一数据类型。
2.19 Sizing arrays
2.20 Arrays of characters
- 强制转换:(类型) 变量名。
- 字符串就是字符数组。
2.21 NUL character
\0
,对应的数字是0。- 字符串最后会加一位
NUL
(占1 byte
),用来表示一个字符串的结束(不然无法知道字符串直之间的开始与结束),方便内存存储。 - 只有字符串会这么做,因为长度不固定,其他类型的长度是定死的,不需要加
NUL(\0)
。
2.22 length.c
2.23 strlen
#include <string.h>
2.24 Manual pages
2.25 ASCII
2.26 string.c
for
循环可以声明多个参数。
|
|
2.27 uppercase.c
2.28 Check for lowercase
2.29 islower
2.30 toupper
2.31 Command-line Arguments
2.32 argc and argv
argc
—> argument countargv
—> argument vector
2.33 argv[0]
|
|
2.34 argv[1]
argv[]
会接受控制台的所有指令。
|
|
2.35 Arrays of arguments
2.36 Exit status
- 没问题返回
0
,有问题返回非0
(即1
)。 main()
方法默认会返回0
。
|
|
2.37 Readability
2.38 Cryptography
2.2 Shorts
2.2.1 Functions








- 组织性
- 简易性
- 复用性











2.2.2 Variables And Scope




- 和
Java
的重要区别! C
中参数传递都是值的拷贝,非地址值。




2.2.3 Arrays



C
中数组越界不一定就报错,要特别谨慎!









C
中,array
(数组)不能直接通过变量赋值,需要循环。


C
中,array
(数组)不是值传递,而是引用传递!
2.2.4 Command Line Arguments

- 命令行参数,新东西,要注意一下。

argc
参数数量;argv
参数数组。

- 注意参数用空格分隔。


2.3 Lab 2
|
|
2.4 Problem Set 2
2.4.1 Readability
|
|
2.4.2 Substitution
|
|
不要一上来就写代码,多想,尤其多考虑边界条件