《C Primer Plus》读书笔记.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《《C Primer Plus》读书笔记.doc》由会员分享,可在线阅读,更多相关《《C Primer Plus》读书笔记.doc(78页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C Primer Plus读书笔记By:riusksk(泉哥)Blog:1 scanf()在读取输入时会自动将空字符0插入字符串末尾,而且当它遇到第一个空白字符空格(blank)、制表符(tab)或者换行符(newline)时会停止读取,因此使用%s的scnaf()只会把一个单词而不是把整个语句作为字符串读入,此时我们一般用gets()来处理一般的字符串。2 字符串常量”x”与字符常量x不同,x属于基本类型(char),而”x”则属于派生类型(char数组),另外,”x”实际上是由两个字符(x和空字符0)组成的。3 strlen()是以字符为单位给出字符串的长度,其中空字符0并不计算在内,而s
2、izeof()是以字节为单位给出数据的大小,其中还包括空字符0。4 定义符号常量的意义:a.提供更多的信息,增强代码的可读性;b.便于更改代码,特别对于在多处使用同一常量而又必须改变它的值时更为适用。可将符号常量名定义为大写字母,当遇到大写的符号名时,就可知道它是一个常量而变量了,比如:#define PI 3.14159这里如果我们这样定义:float pi = 3.14159;由于pi是个变量,程序可能意外地改变它的值,因此我们使用#define来定义它。除了以上方法之外,我们还可以使用const修饰符来创建符号常量,此时它就成为只读值,在计算中是不可改变的,比如:const float
3、pi = 3.14159;5 linits.h:整数限制头文件,float.h:浮点数限制头文件。例如:#include #include #include int main(void)printf(Max int value on this system:%dn,INT_MAX);printf(Min int value on this system:%dn,INT_MIN);printf(Max float normal value on this system:%en,FLT_MAX);printf(Min float normal value on this system:%en,FL
4、T_MIN); return 0;输出结果:Max int value on this system:Min int value on this system:-Max float normal value on this system:3.e+038Min float normal value on this system:1.e-0386 不匹配的浮点转换实例:n1在堆栈中占用8字节(float被转换成double),n2占用8字节,而n3和n4则分别占用4字节,prinft()在读取堆栈中的值时,它是根据转换说明符去读取的。%ld说明符指出,printf()应该读取4个字节,所以prin
5、tf()在堆栈中读取前4个字节作为它的第一个值,即n1的前半部分,它被解释成一个长整数(long integer)。下一个%ld说明符再读取4字节,即n1的后半部分,它被解释成第二个长整数(long integer)。同样,%ld的第三、四个实例使得n2的前半部分和后半部分被读出,并被解释成两个长整数(long integer)。7 printf()返回所打印的字符的数目,如果输出错误,则返回一个负数(旧版本的printf会有不同的返回值),比如:#include int main(void)inttest = 123;intretvalue;retvalue = printf(“the te
6、st value is %dn”,test);printf(“The printf() function printed %d characters.n”,retvalue);return 0;输出结果:the test value is 123The printf() function printed 22 characters.8 在scanf()格式字符串的说明符中,除了%c以外,其它说明符均会自动跳过输入项之前的空格。比如:#include int main(void)int a;printf(enter:n);scanf(%d,&a);printf(%d.n,a);return 0;
7、输出结果:enter: 33.这里并没有输出空格。又比如:#include int main(void)char a;printf(enter:n);scanf(%c,&a);printf(%c.n,a);return 0;输出结果:enter: a .这里就是输出空格了。9 scanf()函数返回成功读入的项目的个数,如果它没有读取任何项目(当它期望一个数字而你又键入一个非数字字符串时就会发生这种情况),scnaf()会返回值0。当它检测到“文件结尾”时,它返回EOF(stdio.h中将EOF定义为值-1)。10 printf()和scanf()的*修饰符:代码一:/-使用可变宽度的输出字段
8、-#include int main(void) unsigned width, precision; int number = 256; double weight = 242.5; printf(What field width?n); scanf(%d, &width); printf(The number is :%*d:n, width, number); printf(Now enter a width and a precision:n); scanf(%d %d, &width, &precision); printf(Weight = %*.*fn, width, preci
9、sion, weight); printf(Done!n); return 0;变量width提供字段宽度,而number就是要打印的数字。其运行结果:What field width?3The number is :256:Now enter a width and a precision:3 7Weight = 242.Done!代码二:/*跳过输入的头两个整数,此功能可用于读取一个文件中某个特定的列*/#include int main(void)int n; printf(please enter three integers:n);scanf(%*d %*d %d,&n);print
10、f(the last integer was %dn,n); return 0;输出结果:please enter three integers:111 222 333the last integer was 33311 取模运算符%只用于整数运算,对于浮点数使用该运算符将是无效的。12 前缀增量与后缀增量的区别:先看下面的代码:#include int main(void) int a = 1, b = 1; int aplus, plusb; aplus = a+; /* 后缀 */ plusb = +b; /* 前缀 */ printf(a aplus b plusb n); print
11、f(%1d %5d %5d %5dn, a, aplus, b, plusb); return 0;运行结果:a aplus b plusb2 1 2 2显然,a和b都加一了,但aplus是a改变之前的值,而plusb却是b改变之后的值。再比如,q=2*+a;它会先将a+1,然后再2*a;而q=2*a+却是先2*a,再将积加1。再举个例子:b = +i /如果使用i+,b会有不同结果,而如果使用下列语句来代替它:+i; /第1行b=i; /如果在第1行使用了i+,b的结果仍会是相同的。13. 增量运算符+与减量运算符具有很高的结合优先级,只有圆括号比它们的优先级高。所以x*y+相当于(x)*(
12、y+)。14. 在y =( 4 + x+)+(6 + x+); 中表达式( 4 + x+)不是一个完整的表达式,所以C不能保证在计算子表达式4 + x+后立即增加x。这里,完整表达式是整个赋值语句,并且分号标记了顺序点,所以C能保证的是在程序进入后续语句前x将被增加两次。C没有指明x是在每个子表达式被 计算后增加还是在整个表达式被计算后增加,这就是我们要避免使用这类语句的原因。15. 请看以下代码:#include int main(void) int i=1; float n;while(i+1时,n都会等于0printf(%fn,n); return 0;16. 请看代码:#include
13、 #define FORMAT %s! C is cool!nint main(void) int num = 10;printf(FORMAT,FORMAT);printf(%dn,+num);printf(%dn,num+);printf(%dn,num-);printf(%dn,num); return 0;运行结果:%s! C is cool! C is cool!1111121117. while循环语句在遇到第一个分号之后就退出循环,例如以下代码:#include int main(void)int n = 0; while (n+ 3); /* line 7 */ printf(
14、n is %dn, n); /* line 8 */ printf(Thats all this program does.n); return 0;输出结果:n is 4Thats all this program does.由于while(n+ 3); 之后存在分号,因此它只是循环的执行n+,直至它小于3才退出循环,相当于一个空语句,退出循环时n刚好等于4。有时,程序员有意地使用带有空语句的while语句,因为所有的工作都在判断语句中进行。例如,你想要跳过输入直到第一个不为空格或数字的字符,可以使用这样的循环:while ( scanf ( “%d”,&num ) = 1);/*跳过整数输
15、入*/只要输入一个整数,则scanf()就返回1,循环就会继续。18. math.h头文件中声明的fabs()函数用于返回一个浮点值的绝对值,即没有代数符号的值。19. 请看代码:#include int main(void) int num = 0;for (printf(keep entering numbers!n);num!=6;)scanf(%d,&num);printf(thats the one I want!n);return 0;输出结果:keep entering numbers!123456thats the one I want!20. houseprice = 249
16、,500; 相当于: houseprice = 249; 500; 而houseprice = (249,500);相当于houseprice =500;21. 求S=1 + 1/2 + 1/4 + 1/8 + 1/16 + #include int main(void) int t_ct;double time,x;int limit;printf(enter the number of terms you want:);scanf(%d,&limit);for (time=0,t_ct=1,x=1;t_ct=limit;t_ct+,x*=2.0)time += 1.0/x;printf(t
17、ime = %f when terms = %d.n,time,t_ct);return 0;运行结果:enter the number of terms you want:15time = 1. when terms = 1.time = 1. when terms = 2.time = 1. when terms = 3.time = 1. when terms = 4.time = 1. when terms = 5.time = 1. when terms = 6.time = 1. when terms = 7.time = 1. when terms = 8.time = 1. w
18、hen terms = 9.time = 1. when terms = 10.time = 1. when terms = 11.time = 1. when terms = 12.time = 1. when terms = 13.time = 1. when terms = 14.time = 1. when terms = 15.22. 代码:#include int main(void)int k;for ( k = 1,printf(“%d:Hi!n”,k) ; printf(“k = %dn”,k),k*k26 ; k+=2,printf ( “Now k is %dn”,k)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C Primer Plus C Primer Plus读书笔记 Plus 读书笔记
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内