CS240 Review

--C 语言 由 Dennis Ritchie 于1972年在 Bell labs发明。
--C 用于部署 UNIX。
操作系统曾100%由 assembly language来部署,导致其依赖硬件结构(architecture dependent)。
C 被发明用来使UNIX portable
95% of UNIX is in C
5% is in assembly (当移植到其他机器时,只有汇编语言部分需要重写)

C 的用途
Linux is written in C
Most of the libraries (low level) that need speed are written in C, strings, Windowing, Graphics, MP3 decoders, math libraries are also written in C
Java runs on top of the JVM and the JVM is written in C
Games that need high performance are written in C (or C++ in some cases or Objective-C)
Java syntax was based on C&C++, therefore, you will find that many of the elements in C you knew them already
C++ is a superset of C, so by learning C, you learn a big chunk of C++.
C is used in Java native libraries that need speed. E.g. User Interface, Access to Database, Animation, Rendering, Math Library etc.

The C Principle
***“No checks and balances for performance” ***
For an assignment to an array element:

a[i]=val;
In Java the code generated is equivalent to:

***// Check boundaries ***
***if (i >= 0 && i < max) { a[i] = val; } else throw out of boundary exception ***
In C the code generated is equivalent to:

***// no boundary checks!!!! ***
***a[i] = val; ***
***Assignment will take place in memory even if i is out of range. ***
***Assignment will happen beyond the end of the array. ***

写 C 的时候,你需要知道你自己在做什么。
与 java相比,C语言编程和调试 debug 更难和更耗时。
Java is used in applications that do not require too much CPU (I/O bound). Example: Web apps, calendar app
C is used in applications that require a lot of CPU (CPU bound). Example: Games, MP3 Player.

I/O bound(I/O 密集型) 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CPU Loading 不高。
CPU bound 指的是系统的 硬盘/内存 效能 相对 CPU 的效能 要好很多,此时,系统运作,大部分的状况是 CPU Loading 100%,CPU 要读/写 I/O (硬盘/内存),I/O在很短的时间就可以完成,而 CPU 还有许多运算要处理,CPU Loading 很高。
计算密集型 (CPU-bound)
在多重程序系统中,大部份时间用来做计算、逻辑判断等CPU动作的程序称之CPU bound。例如一个计算圆周率至小数点一千位以下的程序,在执行的过程当中
绝大部份时间用在三角函数和开根号的计算,便是属于CPU bound的程序。
It is because the performance characteristic of most protocol codec implementations is CPU-bound, which is the same with I/O processor threads.

内存使用
Java 使用垃圾回收机制 Garbage Collection
JVM (java virtual machine) 收集那些程序够不到的对象 unreachable by program.
C 使用 explicit memory management.

与 Java 相比,C的程序一般占用更少的内存。
C 的程序“快且简”, Fast and lean, 但是你写程序的时候需要小心。

Identifiers 标识符
可用字母,下划线 和数字,但不能以数字开头。
只有前37个字符是 significant 的。
大小写混用可以让变量名更可读。

Main 函数
程序从 main 函数开始执行。
int main(int argc, char** argv){
}

argc 保存 args 个数
argv -- it is an array of the argument entries as strings

编辑器:gedit, pico, vim or xemacs.

The for statement is commonly used to iterate a known number of times.

Symbolic constants 符号常量
Example:
**// Print Fahrenheit - Celsius table **
**#include <stdio.h> **
**#define LOWER 0 **
**#define UPPER 300 **

GDB -- Running a Program with gdb
To run a program with gdb type
**gdb progname
(gdb) **
Then set a breakpoint in the main function.
(gdb) break main **
A breakpoint is a marker in your program that will make the program stop and return control back to gdb.
Now run your program.
(
gdb) run **
If your program has arguments, you can pass them after run.

Stepping Through your Program
Your program will start running and when it reaches “main()” it will stop. **gdb> **
Now you have the following commands to run your program step by step: **(gdb) step **
It will run the next line of code and stop. If it is a function call, it will enter the function
**(gdb) next **
It will run the next line of code and stop. If it is a function call, it will not enter the function and it will go through it.
Example:
** (gdb) step
(gdb) next **

Setting breakpoints
You can set breakpoints in a program in multiple ways:
(gdb) break function **
Set a breakpoint in a function E.g.
(gdb) break main **
(
gdb) break line **
Set a break point at a line in the current file. E.g.
(
gdb) break 66 **
It will set a break point in line 66 of the current file.
(gdb) break file:line **
It will set a break point at a line in a specific file. E.g.
(
gdb) break hello.c:78 **

Stopping execution to regain control
When you type
**(gdb) run **
the program will start running and it will stop at a break point.
If no break point is encountered, you can regain control again by typing ctrl-c.

Finding the program line currently being executed
The command
**(gdb)where **
Will print the current function being executed and the chain of functions that are calling that fuction.
This is also called the backtrace.
Example:
**(gdb) where #0 main () at test_mystring.c:22
(gdb) **

Debugging a Crashed Program
This is also called “postmortem debugging”
When a program crashes, it writes a core file.
**bash-4.1$ ./hello
Segmentation Fault (core dumped)
bash-4.1$ **
The core is a file that contains a snapshot of the program at the time of the crash. That includes what function the program was running.

Paste_Image.png

Count Characters in a file

#include <stdio.h> 
main() 
{ 
     int nc = 0; 
     while (getchar() != EOF) { 
        nc++; 
     } 
  printf(“Number of characters: %d\n”, nc); **
  } 

**gcc –g –o chcount chcount.c **
**./chcount **
**Hello **
**World **
**<ctrl-d> **
**Number of characters: 11 **
**./chcount < myfile.txt **
**Number of characters: 37 (assuming myfile.txt has 37chars) **

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容