【入門】C程序設計(第三版)譚浩強—第三章

   第三章要點整理圖:

C程序設計(第三版)譚浩強—第三章

 

     第三章程序整理:

  1. 例題3.1 
  2. #define PRICE 30                //預定義符號常量PRICE 
  3. #include<stdio.h> 
  4. void main() 
  5.     int num,total; 
  6.     num=10; 
  7.     total=num*PRICE; 
  8.     printf("total=%d\n",total); 
  9.  
  10. 例題3.2 
  11. #include<stdio.h> 
  12. void main() 
  13.     int a,b,c,d;                    /*指定a、b、c、d爲整型變量*/ 
  14.     unsigned u;                     /*指定u爲無符號整型變量*/ 
  15.     a=12;b=-24;u=10; 
  16.     c=a+u;d=b+u;                    /*u轉爲有符號時爲10*/ 
  17.     printf("a+u=%d,b+u=%d\n",c,d); 
  18.  
  19. 例題3.3 
  20. #include<stdio.h> 
  21. void main() 
  22.     int a,b; 
  23.     a=32767; 
  24.     b=a+1;                  /*若運行在16位機上,b將溢出*/ 
  25.     printf("%d,%d\n",a,b); 
  26.  
  27. 例題3.4 
  28. #include<stdio.h> 
  29. void main() 
  30.     float a,b; 
  31.     a=123456.789e5;  
  32.     b=a+20;  
  33.     printf("%f\n",b);   /*結果都是:12345678848.000000*/ 
  34.                         /*前8位準確,後幾位不準確【加20無意義】*/ 
  35.  
  36. 例題3.5 
  37. #include<stdio.h> 
  38. void main() 
  39.     printf(" ab c\t de\rf\tg\n");   /*\t水平製表\r回車\n換行*/ 
  40.     printf("h\ti\b\bj k\n");        /*\b退格*/ 
  41.     //gcc編譯結果顯示如下: 
  42.     //fab c   gde 
  43.     //h      j k 
  44.  
  45. 例題3.6 
  46. #include<stdio.h> 
  47. void main() 
  48.     char c1,c2; 
  49.     c1=97;                      /*字符a*/ 
  50.     c2=98;                      /*字符b*/ 
  51.     printf("%c %c\n",c1,c2);    /*a b*/ 
  52.     printf("%d %d\n",c1,c2);    /*97 98*/ 
  53.  
  54. 例題3.7 
  55. #include<stdio.h> 
  56. void main() 
  57.     char c1,c2; 
  58.     c1='a'
  59.     c2='b'
  60.     c1=c1-32;               /*轉爲A*/ 
  61.     c2=c2-32;               /*轉爲B*/ 
  62.     printf("%c %c",c1,c2); 
  63.  
  64. 例題3.8 
  65. #include<stdio.h> 
  66. void main() 
  67.     float x; 
  68.     int i; 
  69.     x=3.6; 
  70.     i=(int)x;                   /*i爲3,x不變*/ 
  71.     printf("x=%f,i=%d\n",x,i); 
  72.  
  73. 例題3.9 
  74. #include<stdio.h> 
  75. void main() 
  76.     unsigned a; 
  77.     int b=-1; 
  78.     a=b;                /*32位機中a存儲爲32個1,2的32次方-1*/ 
  79.     printf("%u\n",a); 
  80.  
  81. 習題3.6 
  82. #include<stdio.h> 
  83. void main() 
  84.     char c1='a',c2='b',c3='c',c4='\101',c5='\116'
  85. /*c1 a;c2 b;c3 c;c4 八進制101 十進制65 A;c5 八進制116 十進制78 N*/ 
  86.     printf("a%c b%c\tc%c\tabc\n",c1,c2,c3); 
  87.     printf("\t\b%c %c\n",c4,c5); 
  88.  
  89. 習題3.7 
  90. #include<stdio.h> 
  91. void main() 
  92.     char c1,c2,c3,c4,c5; 
  93.     c1='C';c2='h';c3='i';c4='n';c5='a'
  94.     c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4; 
  95.     printf("%c %c %c %c %c",c1,c2,c3,c4,c5); 
  96.  
  97. 習題3.8 
  98. #include<stdio.h> 
  99. void main() 
  100.     int c1,c2; 
  101.     c1=97;                      /*整數97*/ 
  102.     c2=98;                      /*整數98*/ 
  103.     printf("%c %c\n",c1,c2);    /*a b*/ 
  104.     printf("%d %d\n",c1,c2);    /*97 98*/ 
  105.  
  106. 習題3.10 
  107. #include<stdio.h> 
  108. void main() 
  109.     int i,j,m,n; 
  110.     i=8; 
  111.     j=10; 
  112.     m=++i;          /*i=9,m=9*/ 
  113.     n=j++;          /*j=10,n=10,j=11*/ 
  114.     printf("%d,%d,%d,%d\n",i,j,m,n); 

書中調試用Turbo C,int爲16位。但在32位機上,gcc中的int爲32.故執行結果與書中有出入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章