--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.
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) **