第三章要點整理圖:
第三章程序整理:
- 例題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.故執行結果與書中有出入。