CS61C 2020计算机组成原理Lecture02

1、Compilation

C is a compiled language.

C compilers map C programs into architecture-

specific machine code (string of 0s and 1s)

  • Unlike Java, which converts to architecture-independent bytecode (run by JVM)

  • Unlike python, which directly interprets the code

  • Main difference is when your program is mapped to low-level machine instructions

ps:C 程序是在CPU上加载并且执行的,所以速度很快

在这里插入图片描述
在这里插入图片描述

2、Typed Variables in C

  • Integer sizes are machine dependant!
  • Common size is 4 or 8 bytes (32/64-bit), but can’t ever assume this
  • Can add“unsigned” before int or char

Characters

ASCII standard defines 128 different characters and their numeric encodings

A char takes up 1 byte of space

  • 7 bits is enough to store a char (2 ^ 7 = 128), but we add a bit to round up to 1 byte since computers usually deal with multiples of bytes

Typecasting in C

在这里插入图片描述

Structs Alignment and Padding in C

在这里插入图片描述

Unions in C

在这里插入图片描述
联合体与结构体之间的区别是:结构体的各个成员会占用不同的内存,相互之间无影响。而联合体的所有成员公用一段内存,修改一个成员会影响其余所有成员。

结构体占用的内存大于等于所有成员占用的内存的总和(成员之间可能会存在缝隙),联合体占用的内存等于最长的成员占用的内存。

联合体使用了内存覆盖技术,同一时刻只能保存一个成员的值,如果对新的成员赋值,就会把原来成员的值覆盖掉。

C 和 java的区别

在这里插入图片描述
特别注意,c中要自己手动管理内存。

3. C Syntax and Control Flow

在这里插入图片描述

C Syntax:main

在这里插入图片描述

main Example

在这里插入图片描述
打印argv[3],结果是 null 或者 zero

C Syntax: Variable Declarations

如果某个变量被申明了但是没进行初始化,那么之后你直接引用它的话会得到垃圾值

c语言中是没有布尔类型的

Has there been an update to ANSI C?

在这里插入图片描述

4. Pointers — Address vs. Value

A pointer is a variable that contains an address

  • An address refers to a particular memory location,usually also associated with a variable name
  • Name comes from the fact that you can say that it points to a memory location

在这里插入图片描述

Pointer Syntax

在这里插入图片描述

Pointers in C

在这里插入图片描述

Pointer Bugs

在这里插入图片描述

Summary

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值