《The C programming language》
"Author Brian W.Kernighan,Dennis M.Ritchie
Introduction
Chapter1 totorial on the central part of C
"The purpose is to get the reader started as quickly as possible,since we believe strongly that
the way to learn a new language is to write programs int it.
APPENDIX B:Standard Library
"when a program begins execution,the three streams stdin,stdout,and stderr are already open."
The syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear.This
reasoning applies to function declarations as well.For example,
double *dp,atof(char *);
says that in an expression *dp and atof(s) have values of type double,and that the argument of atof is a pointer to char.
"
最近在看从laylor那儿借来的英文版The C Programming Language,有不小的振撼,虽说,C应该算是最最基础的了,对于一个学计算机四年了的人来说,再看这种基础是不是有点事倍功半?我不知道,何必要求结果,有巨大的收获与振撼就说明总有益处。这本书除了给人对C的语法有清晰地说明外(而且还真是面面俱到,确是身边应有的手册),更重要的是其示例代码及其一些慢慢植入专业智慧的思想,其代码毫无托泥带水,同时,其思想来源及其代码简化思路、原因可以说,总让人感叹“国外的书与国内的书,水平就是相差悬殊 ”,好了,不多感叹了,把我认为的收获与自己风格的笔记留下,不求闻达于程序猿而已,只是自己笔记,自己回味而已。
//getint.c
/*function:performs free-format input input conversion by breaking a stream of characters into integer values,one integer per call.
getint function has to return the value it found and also signal end of file when there is no more input.These values have to be passed back by separate posths,for no matter what value is used for EOF,that could also be the value of an input integer.One solution is to have getint return the end of file status as its function value,while using a pointer argument to store the converted integer back in the calling function.This is the scheme used by scanf as well.注意:这里其实强调的是传址调用*/
#include
#include
/*declares fuctions testing characters*/
int getch(void);void ungetch(int);
/*getint: get next integer from input into *pn*/
int getint(int *pn)
{int c,sign;
while(isspace(c=getch()))/*skip white space*/;
if(!isdigit(c)&&c!=EOF&&c!='+'&&c!='-')
{ungetch(c);/*it's not a number*/return 0;}
sign=(c=='-')? -1:1;if(c=='+'||c=='-')c=getch();
for(*pn=0;isdigit(c);c=getch())
*pn=10* *pn+(c-'0');
*pn*=sign;if(c!=EOF)
ungetch(c);
return c;/*the last character*/
}
#define SIZE 10
int main(void){
int n,array[SIZE];
for(n=0;n<SIZE&&getint(&array[n])!=EOF;n++);
for(n=0;n<SIZE&&array[n];n++)
printf("array's value are %d\n",array[n]);
return 0;
}
//getchOungetch.c
/*It is often the case that a program cannot determine that it has read enough input until it has read too much.One instance is collecting the characters that make up a number:until the first nondigit is seen,the number is not complete.But then the program has read one character too far,a character that it is not prepared for.The problem would be solved if it were possible to "un-read" the unwanted character.Then,every time the program reads one character too many,it could push it back on the input,so the rest of the code could behave as if it had never been read.Fortunately,it's easy to simulate un-getting a character ,by writing a pair of cooperating functions.getch() delivers the next input character to be considered;ungetch() remembers the characters put back on the input,so subsequent calls to getch will return them before reading new input.*/
//#include
#define BUFFSIZE 10
char buf[BUFFSIZE];/*buffer for ungetch*/
int bufp=0;/*next free position in buf*/
int getch(void)/*get a (possibly pushed back) character*/
{return (bufp>0)? buf[--bufp]:getchar();}
void ungetch(int c)/*push character back on input*/
{
if(bufp>=BUFFSIZE)
{printf("ungetch:too many characters\n");}
else
buf[bufp++]=c;
}
还有,你平时注意到写得简洁吗?比如下面两个的比较:
/*strcpy:copy t to s;pointer version 2*/
void strcpy(char *s,char *t)
{
while((*s++=*t++)!='\0');
}
/*strcpy:copy t to s;pointer version 3*/
void strcpy(char *s,char *t)
{
while((*s++=*t++));
}
书上说得理由让人回味:observe that a comparison against '\0' is redundant,since the question is merely whether the expression is zero.
*p++=val;/*push val onto stack*/
val=*--p;/*pop top of stack into val*/