The C Programming Language 读书笔记(Chapter 1)

来源:互联网 发布:淘宝特产店名大全 编辑:程序博客网 时间:2024/05/16 19:40


In this Chapter, a lot of examples are presented to introduce the basics used to write useful programs.
包括:Variables and Arithmetic, the for statement, Symbolic constants, Character input and output, Arrays,
Functions, Arguments, Character Arrays, External Variables.
这些知识很多知识复习而已。编了这么久的程序,这些知识还是掌握了。

这章包含了许多基本的概念,主要有:
functions, variables, statements, arguments, character string (= string constant)
loops, formatted output,  symbolic constants (使用#define以去掉programs中的magic Numbers)
text stream, character constants (between single qutote), Arrays, function prototype,
call by value, call by reference, external variables and scope, automatic variables.


文件处理的常见模式char by char为:
int c;
while ((c = getchar()) != EOF) {
 if (c 满足条件) {
  ... // 处理
 }
 ... // putchar(c);
}
文本处理常见模式word by word为:
int c, state;

state = OUT;
whiel ((c = getchar()) != EOF) {
 if (c == ' ' || c == '/n' || c == '/t')
  state = OUT;
 else if (state == OUT) {
  state = IN;
 }
 
 if (state == IN) {
  ... // c为当前word中的字符
 }
}

文本处理常见模式line by line为:
#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);

int len; /* current line length */
char line[MAXLINE]; /* current input line */

while ((len = getline(line, MAXLINE)) > 0) {
 ... // 处理line
}

/* 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!='/n';++i)
  s[i] = c;
 if (c == '/n') {
  s[i] = c;
  ++i;
 }
 s[i] = '/0';
 return i;
}
动词:
write (programs)
create, compile, load, run (programs)
on the Unix operating system
type (command)
print
call (functions)
define (function, variables)
declare (...)
in braces ({})
indent (statements)
read, write (characters, lines)
return (values)
execute
terminate