The C Programming Language,C程序设计语言 (K&R),为C语言的设计者Dennis M. Ritchie和著名的计算机科学家Brian W.Kernighan合著的 一本介绍C语言的权威经典著作,学习c语言至今,第一次读这本书,这本书适合有一定的c语言基础的深入学习。
为什么说不适合初学者,觉得这本书更像一本字典,比如在函数章节,举例代码中用到了下面章节的数组,用到了数据结构的栈知识,为了学习大师的代码,在读这本书的时候,书中引用的代码,一一记录如下
第一章 导言
1、华氏温度与摄氏温度对照表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* 当 fahr=0,20,… ,300 时,分别 打印华氏温度与摄氏温度对照表 */ int main() { int fahr, celsius; int lower, upper, step; lower = 0; /* 温度表的下限 */ upper = 300; /* 温度表的上限 */ step = 20; /* 步长 */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d %d ", fahr, celsius); fahr = fahr + step; } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300; floating-point version */ int main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperatuire scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f ", fahr, celsius); fahr = fahr + step; } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300; floating-point version */ int main() { int fahr; for (fahr = 0; fahr <= 300; fahr = fahr + 20) printf("%3d %6.1f ", fahr, (5.0/9.0)*(fahr-32)); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #define LOWER 0 /* lower limit of table */ #define UPPER 300 /* upper limit */ #define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */ int main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f ", fahr, (5.0/9.0)*(fahr-32)); return 0; }
2、文件复制,getchar 与 putchar 函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* copy input to output; 1st version */ int main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* copy input to output; 1st version */ int main() { int c; while ((c = getchar()) != EOF) putchar(c); return 0; }
3、字符计数,double也可以自增
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* count characters in input; 1st version */ int main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld ", nc); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* count characters in input; 2nd version */ int main() { double nc; for (nc = 0; getchar() != EOF; ++nc) ; printf("%.0f ", nc); return 0; }
4、行计数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* count lines in input */ int main() { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == ' ') ++nl; printf("%d ", nl); return 0; }
5、单词计数,符号常量
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input */ int main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == ' ') ++nl; if (c == ' ' || c == ' ' || c == ' ') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d ", nl, nw, nc); return 0; }
6、统计数字、空白符、其他字符,利用数组统计数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> /* count digits, white space, others */ int main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']; /*count digits*/ else if (c == ' ' || c == ' ' || c == ' ') ++nwhite; else ++nother; printf("digits ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d ", nwhite, nother); return 0; }
7、求幂的函数 power(m, n) ,传值调用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> int power(int m, int n); /* test power function */ int main() { int i; for (i = 0; i < 10; ++i) printf("%d %d %d ", i, power(2,i), power(-3,i)); return 0; } /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> int power(int m, int n); /* test power function */ int main() { int i; for (i = 0; i < 10; ++i) printf("%d %d %d ", i, power(2,i), power(-3,i)); return 0; } /* power: raise base to n-th power; n >= 0; version 2 */ int power(int base, int n) /*传值*/ { int p; for (p = 1; n > 0; --n) p = p * base; return p; }
8、输出最长字符串,字符数组,getline,copy,外部变量
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print the longest input line */ int main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: read a line into s, return length */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=' '; ++i) s[i] = c; if (c == ' ') { /*长度包含回车符*/ s[i] = c; ++i; } s[i] = '