1 | "hellow world\n" |
双引号包起来的叫做 character string 或者 string constant
C 中所有的变量必须在使用前声明,通常在函数的开头位置,其他语句之前,声明语句宣布了变量的属性。
格式化
| formating | desc |
|---|---|
| %d | print as decimal integer |
| %6d | print as decimal interger, at least 6 characters wide |
| %6f | print as floating point, at least 6 characters wide |
| %f | print as floating point |
| %.2f | print as floating point, 2 characters after decimal point |
| %6.2f | print as floating point, at least 6 wide and 2 after decimal point |
symbolic constants
符号常量,不需要声明,一般大写,用于区分小写的变量。#define PI 3.1415
character input and output
The model of input and output supported by the standard library is very simple. Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters. A text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character. It is the responsibility of the library to make each input or output stream confirm this model; the C programmer using the library need not worry about how lines are represented outside the program.
getchar()/putchar()
for(int i=0;i<10;++i);null statement
赋值表达式
Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.
赋值表达式是有返回值的。
nl = nw = nc = 0; 相当于 nl = (nw = (nc = 0 )) right to left
在 C 语言中,赋值表达式有返回值,这是 C 语言的一个核心特性。这使得赋值表达式既能执行赋值操作,也能返回赋值结果,从而允许赋值操作与其他逻辑结合使用。
赋值表达式返回值的规则
返回值等于被赋的值:赋值表达式 x = y 的返回值是赋值操作完成后变量 x 的新值,也就是 y 的值。
赋值表达式的类型:赋值表达式的类型是左操作数(即被赋值的变量)的类型。例如:
1 | int x; |
1 |
|
连续赋值
1 |
|
赋值表达式的实际应用
结合条件语句 赋值表达式返回值的特性允许在条件语句中直接进行赋值并判断其结果。
1
2
3
4
5
6
7
8
9
int main() {
int x;
while ((x = getchar()) != EOF) { // 将 getchar() 的返回值赋给 x 并判断是否为 EOF
putchar(x); // 输出读取的字符
}
return 0;
}结合复杂逻辑 可以在一行中完成多个变量的赋值和比较:
1 |
|
注意事项
运算优先级: 赋值操作符 = 的优先级较低(比逻辑操作符 == 和算术操作符 +、- 低),所以需要用括号明确优先级。if (x = 5 == y) // 等价于 if (x = (5 == y)) 如果希望先赋值,再进行比较,需要添加括号:if ((x = 5) == y) 明确优先级,先赋值 x,再比较 x 和 y。
初学者常常误用 =(赋值)代替 ==(比较)。如:
1 | if (x = 1) { // 实际上是赋值操作,而不是判断 x 是否等于 1 |
为了防止这种错误,可以使用编译器警告或更规范的代码写法(如常量写在左侧:if (1 == x))。
在 C 语言中,赋值表达式会返回被赋的值,这使得它既能执行赋值操作,又能用于条件判断或连续赋值。合理利用赋值表达式的返回值特性,可以编写简洁高效的代码,但要注意括号优先级和避免逻辑错误。
注意:表达式是有返回值的,java 也一样,但是 python 不能,python 的表达式是语句,不能用在条件判断中,python 3.8 引入了 赋值表达式(海象运算符 :=)
表达式和语句
在编程中,表达式和语句是两个基本概念,但它们的用途和功能不同。
以下是它们的详细区别:
表达式(Expression)
- 定义:表达式是具有值的代码片段。它可以进行计算,并返回一个结果。
- 用途:用来计算、操作数据、返回结果,可以嵌套在其他表达式或语句中。
- 组成:可以由变量、常量、操作符、函数调用等组成。
特点
- 表达式会返回一个值。
- 表达式可以嵌套在语句中。
- 表达式的值可以被赋值给变量,或者直接参与运算。
1
2
3
4
5
6
7
8
9// 表达式示例(C语言)
int x = 5; // 这里的 "5" 是表达式,返回值是 5
int y = x + 3; // "x + 3" 是表达式,返回值是 8
printf("%d", y); // 函数调用 printf() 是一个表达式,返回值是字符数
// 表达式示例(Python)
x = 5 # "5" 是表达式
y = x * 2 # "x * 2" 是表达式,返回值是 10
print(y > 5) # "y > 5" 是表达式,返回值是 True语句(Statement)
- 定义:语句是一条完整的指令,用来执行某种操作。它通常不返回值,而是改变程序的状态(如赋值、控制流程等)。
- 用途:控制程序的行为(如条件判断、循环、赋值)。
- 组成:可以包含一个或多个表达式。
特点
- 语句通常不返回值(部分语言中例外,比如 Python 的赋值表达式语句)。
- 语句是程序的基本单元,执行操作,但通常不嵌套在其他语句中。
- 语句可以包含表达式。
1
2
3
4
5
6
7
8
9
10// 语句示例(C语言)
int x = 5; // 赋值语句
if (x > 3) { // 条件语句
printf("Yes"); // 函数调用语句
}
// 语句示例(Python)
x = 5 # 赋值语句
if x > 3: # 条件语句
print("Yes") # 函数调用语句
表达式和语句的对比
| 特性 | 表达式 | 语句 |
|---|---|---|
| 定义 | 代码片段,返回值。 | 完整指令,用于执行操作。 |
| 是否返回值 | 必须有返回值。 | 通常无返回值(少数例外,如 Python 中 :=) |
| 作用 | 计算值或进行操作,返回一个结果。 | 改变程序状态或控制程序执行流。 |
| 嵌套能力 | 可以嵌套在其他表达式或语句中。 | 通常不能嵌套在其他语句中. |
| 组成 | 变量、常量、操作符、函数调用等。 | 表达式和控制结构(如 if、while 等)。 |
| 示例 | x + y、3 > 2 | x = 5;、if (x > 3) {…} |
1 | int x = 5; // 整体是语句,其中 "5" 是表达式 |
1 | x = 5 # 赋值语句,其中 "5" 是表达式 |
总结
- 表达式更关注计算和返回值,可以作为更复杂结构的一部分。
- 语句更关注完成某种操作或控制流程,是程序逻辑的构建块。
两者经常结合使用:语句中包含表达式,表达式的结果用于控制语句的执行逻辑。
写一个wc程序
functions
int sum(int i, int);int sum(int, int)
function prototype
parameter
formal argument
actual argument
old version function declare and define
不再推荐使用,编辑器不能做检查
1 | // 声明 |
Arguments - Call by Value
External Variables and Scope
函数内部的变量
local variables == automatic variables
函数外的变量在函数内部使用需要用关键字 extern 声明
一般不使用 extern, 通常做法是把变量放在文件开头,然后在函数里面使用。
In fact, common practice is to place definitions of all external variables at the beginning of the source file, and then omit all extern declarations.