【入门】C程序设计(第三版)谭浩强—第三章
第三章要点整理图:
第三章程序整理:
- 例题3.1
- #define PRICE 30 //预定义符号常量PRICE
- #include<stdio.h>
- void main()
- {
- int num,total;
- num=10;
- total=num*PRICE;
- printf("total=%d\n",total);
- }
- 例题3.2
- #include<stdio.h>
- void main()
- {
- int a,b,c,d; /*指定a、b、c、d为整型变量*/
- unsigned u; /*指定u为无符号整型变量*/
- a=12;b=-24;u=10;
- c=a+u;d=b+u; /*u转为有符号时为10*/
- printf("a+u=%d,b+u=%d\n",c,d);
- }
- 例题3.3
- #include<stdio.h>
- void main()
- {
- int a,b;
- a=32767;
- b=a+1; /*若运行在16位机上,b将溢出*/
- printf("%d,%d\n",a,b);
- }
- 例题3.4
- #include<stdio.h>
- void main()
- {
- float a,b;
- a=123456.789e5;
- b=a+20;
- printf("%f\n",b); /*结果都是:12345678848.000000*/
- /*前8位准确,后几位不准确【加20无意义】*/
- }
- 例题3.5
- #include<stdio.h>
- void main()
- {
- printf(" ab c\t de\rf\tg\n"); /*\t水平制表\r回车\n换行*/
- printf("h\ti\b\bj k\n"); /*\b退格*/
- //gcc编译结果显示如下:
- //fab c gde
- //h j k
- }
- 例题3.6
- #include<stdio.h>
- void main()
- {
- char c1,c2;
- c1=97; /*字符a*/
- c2=98; /*字符b*/
- printf("%c %c\n",c1,c2); /*a b*/
- printf("%d %d\n",c1,c2); /*97 98*/
- }
- 例题3.7
- #include<stdio.h>
- void main()
- {
- char c1,c2;
- c1='a';
- c2='b';
- c1=c1-32; /*转为A*/
- c2=c2-32; /*转为B*/
- printf("%c %c",c1,c2);
- }
- 例题3.8
- #include<stdio.h>
- void main()
- {
- float x;
- int i;
- x=3.6;
- i=(int)x; /*i为3,x不变*/
- printf("x=%f,i=%d\n",x,i);
- }
- 例题3.9
- #include<stdio.h>
- void main()
- {
- unsigned a;
- int b=-1;
- a=b; /*32位机中a存储为32个1,2的32次方-1*/
- printf("%u\n",a);
- }
- 习题3.6
- #include<stdio.h>
- void main()
- {
- char c1='a',c2='b',c3='c',c4='\101',c5='\116';
- /*c1 a;c2 b;c3 c;c4 八进制101 十进制65 A;c5 八进制116 十进制78 N*/
- printf("a%c b%c\tc%c\tabc\n",c1,c2,c3);
- printf("\t\b%c %c\n",c4,c5);
- }
- 习题3.7
- #include<stdio.h>
- void main()
- {
- char c1,c2,c3,c4,c5;
- c1='C';c2='h';c3='i';c4='n';c5='a';
- c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;
- printf("%c %c %c %c %c",c1,c2,c3,c4,c5);
- }
- 习题3.8
- #include<stdio.h>
- void main()
- {
- int c1,c2;
- c1=97; /*整数97*/
- c2=98; /*整数98*/
- printf("%c %c\n",c1,c2); /*a b*/
- printf("%d %d\n",c1,c2); /*97 98*/
- }
- 习题3.10
- #include<stdio.h>
- void main()
- {
- int i,j,m,n;
- i=8;
- j=10;
- m=++i; /*i=9,m=9*/
- n=j++; /*j=10,n=10,j=11*/
- printf("%d,%d,%d,%d\n",i,j,m,n);
- }
书中调试用Turbo C,int为16位。但在32位机上,gcc中的int为32.故执行结果与书中有出入。
转载于:https://blog.51cto.com/huaxia283611/1057544