1 / 290

書名:輕鬆學習 C 語言 TOC

書名:輕鬆學習 C 語言 TOC. 作者:陳澤雄、蕭宗志、林國任、黃珮瑩、黃佑民 出版社:旗標出版股份有限公司 CIT F.4: 2-9,12,14,18-152,160,167-201. 第一章 C 語言簡介. 1-1 C 語言的結構 1-2 識別字 // identifier 1-3 關鍵字 // reserved words 1-4 註解 // comments /* remarks */ 1-5 前端處理程式 #include, #define. 1-1 C 語言程式的結構. C 語言程式的基本架構如下:. // test.c

Télécharger la présentation

書名:輕鬆學習 C 語言 TOC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 書名:輕鬆學習C語言TOC • 作者:陳澤雄、蕭宗志、林國任、黃珮瑩、黃佑民 • 出版社:旗標出版股份有限公司 CIT F.4: 2-9,12,14,18-152,160,167-201

  2. 第一章 C語言簡介 • 1-1 C語言的結構 • 1-2 識別字 // identifier • 1-3 關鍵字 // reserved words • 1-4 註解 // comments /* remarks */ • 1-5 前端處理程式 #include, #define

  3. 1-1 C語言程式的結構 • C 語言程式的基本架構如下: // test.c #include<stdio.h> /*前端處理*/ int main(){ /*主程式*/ … 程式內容 … system("pause"); // 暫停 return 0; } // test.cpp #include<iostream> using namespace std; int main(){ … 程式內容 … system("pause"); return 0; }

  4. Ch1_1 C的基本架構 Ch1_1 ─ 計算n!的值 1 #include<stdio.h> //前端處理程式 2 main(){ 3 4 5 6 7 8 9 10 11 } 主程式 int i, n, sum=1; //變數宣告 printf("The input number ="); scanf("%i",&n); for (i=1; i<=n; i++) { sum=sum*i; } printf("The result of %i! is :%i\n",n, sum); 輸出: The input number = 4 The result of 4! is : 24 指令敘述

  5. 1-2 識別字(Identifier) • C 語言程式碼中使用的變數或常數名稱, • 其命名固然可依使用者的喜好而定, • 但仍然存在某些限制,不得擅取。分述如下: 1. 只能使用英文字母(A-Z)、阿拉伯數字(0-9)以及底線符號( _ )。 2. 第一個字母必須為英文字母或是底線符號。 3. 字母的大小寫,分別代表不一樣的識別字。 4. 不鼓勵以底線符號作為變數(Variable)名稱的首字。 5. 以底線開頭的識別字,大都為系統所使用。 6. 識別字最長可達31個字元。 7. 不可使用關鍵字(保留字)作為識別字。

  6. 識別字(Identifier)

  7. Ch1_2 變數variable Ch1_2 名稱的使用 1 #include<stdio.h> 2 main(){ 3 intans; 4 ans = 6*9; 5 printf("The answer is %i.\n", ans); 6 } 變數 The answer is 54.

  8. 1-3 關鍵字(Keyword)、保留字 • 關鍵字在C 語言中具有特殊意義,不能將這些字當作識別字來使用,不能拿來當作一般變數或常數名稱使用。

  9. 1-4 註解(Comment) • 註解為符號『/*』和符號『*/』中間所包含的字元所組合而成的,可放置於程式的任意部位。 • 註解的有無或內容為何都不會影響程式的執行,C 語言編譯器會把註解當作一個空白字元,而不會去編譯它。 • 註解的功用在於增加程式的可讀性。 • 註解格式如下:

  10. 1-5 前端處理程式 (Preprocessor) • 在C 語言中,只要前端有『#』符號者,皆是。 • 其位置皆放在函數之外、程式的前面。 • C 語言的前端處理程式如下:

  11. 1-5-1 #define 前端處理程式 大寫 #define PI 3.14 #define f(x)((x)*(x)) 沒=沒; #define SCH "abc"

  12. Ch1_3 #define 之應用 輸出結果 10+21=31 10-21=-11 Ch1_3 ─定義常數 1 #include<stdio.h> 2 #define A10 3 #define B21 4 main(){ 5 printf("%i+%i=%i\n", A,B, A+B); 6 printf("%i-%i=%i\n", A,B, A-B); 7 }

  13. Ch1_4 #define之應用 x²+3x3 Ch1_4 ─定義數學運算式 1 #include<stdio.h> 2 #define f(x)((x)*(x)+3*(x)-3) 3 #define Y 2 4 main(){ 5 printf("f(%i)=%i\n", Y, f(Y)); 6 } f(2)=7

  14. Ch1_5 #define之應用 簡寫 Ch1_5 ─定義字串或字元 1 #include<stdio.h> 2 #define S "The abbreviation is " 3 #define C 'n' 大寫 4 main(){ 5 printf("%s %c.\n", S,C); 6 } The abbreviation isn.

  15. 1-5-2 #undef • 使用來終止之前被#define所定義的變數。 • 語法 • #undef 名稱

  16. Ch1_6 # undef之應用 Ch1_6 ─計算1+2+…+9+10 1 #include<stdio.h> 2 #define N 99 3 main(){ 4 int i, sum=0; 5 #undef N 6 #define N 11 7 for(i=1; i<N; i++) sum+=i; 8 printf("sum=%i\n", sum); 9 } 定義N=99 終止定義N 重新定義N sum=55

  17. 1-5-3 #include • 表示對於程式外的標頭檔的引含指令,通常所引含的檔案都是以.h來作為擴充檔名。 • C語言編譯器都提供其系統定義的標頭檔 header • 標頭檔的內容大多為巨集定義及函數型式。 • 語法一 • #include <system-filename> • 語法二 • #include "user-filename"

  18. 第二章 C語言的基本資料型態 • 2-1 常數 const float PI=3.14; • 2-2 變數 float radius=10; • 2-3 資料型態

  19. 2-1 常數(Constant) • 資料在程式執行過程中,其內容始終維持不變 • 數值常數(Numeric Constant) • 整數常數(Integer Constant) • 浮點常數(Floating-point Constant • 字元常數(Character Constant) • 字串常數(String Constant)

  20. 2-1-1 整數常數 • 在程式執行過程中,資料內容始終維持不變。 • 十進位(Decimal) 例如: 21 • 逢10進1,使用0~9。 • 八進位(Octal) 例如: 025 • 逢 8進1,使用0~7。 • 十六進位(Hexadecimal) 例如: 0x15 • 逢16進1,使用0~9,A~F。

  21. Ch2_1 整數常數表示法 1110  0118 0x1116 %i 10進 %o 8進 %x 16進 Ch2_1 1 #include<stdio.h> 2 main(){ 3 printf("The decimal of 11 is %i.\n", 11 ); 4 printf("The decimal of 011 is %i.\n", 011 ); 5 printf("The decimal of 0x11 is %i.\n", 0x11 ); 6 } The decimal of 11 is 11. The decimal of 011 is 9. The decimal of 0x11 is 17. decimal =十進制

  22. 2-1-2 浮點float常數 • 帶有小數點的數,包括正、負數。 • 浮點常數的表示法如下: • 小數點表示法(decimal notation)。 • 科學符號表示法(scientific notation),又稱為指數表示法(exponential notation)。 m×10n

  23. Ch2_2 浮點常數表示法 Ch2_2 1 #include<stdio.h> 2 main(){ 3 printf("The decimal notation is %f.\n", 6.9102119); 4 printf("The scientific notation is %e.\n", 6.9102119); 5 } 6.910212×100 The decimal notation is 6.910212. The scientific notation is 6.910212e+00.

  24. 2-1-3 字元常數 用一對單引號將字元框起來 • 英文字母 'A','a' • 數字 '0','9' • 控制字元(特殊) '\t','\n','\a' • 其他符號'+','-','!' 參閱ASCII 字符表

  25. Ch2_3 字元與ASCII Code Ch2_3 字元與ASCII Code值的轉換 1 #include<stdio.h> 2 main(){ 3 printf("The ASCII Code of %c is %i.\n", 'A','A'); 4 printf("The ASCII Code of %c is %i.\n", 97,97); 5 printf("%i != %i\n", '0', 0); 6 } The ASCII Code of A is 65. The ASCII Code of a is 97. 48 != 0

  26. C語言控制字元表:

  27. Ch2_4 控制字元的使用 Ch2_4 1 #include<stdio.h> 2 main(){ 3 printf("Hong Kong %cUniversity", '\n'); 4 } Hong Kong University

  28. 2-1-4 字串常數 "String" 用一對雙引號將文字框起來 • 為一串字元組合而成,其個數並沒有嚴格的限制。 • 必須使用一對雙引號將字元框起來。 • 字串是字元陣列(array of char)。

  29. Ch2_5 字串常數表示法 Ch2_5 1 #include<stdio.h> 2 main(){ 3 char s[18]="Taiwan University"; 4 printf("The memory space of the string is %i bytes.\n", sizeof(s)); 5 } 雙引號 字串長度 bytes The memory space of the string is 18 bytes.

  30. 2-2 變數(Variable) • 資料的內容會隨著程式的執行而有所改變。 • 是一個記憶體空間,可分成 • 數值變數 (Numeric variable) • 整數變數 (Integer variable) • 浮點變數 (Floating-point variable) • 字元變數 (Character variable) • 字串變數 (String variable)

  31. 2-2-1 整數變數 (int) • 整數宣告包含 • 整數(int) • 正整數 (unsigned int) • 負整數 (signed int) • 短整數 (short int) • 長整數 (long int) • 語法一int a=6, b=9; • 語法二int answer;

  32. 2-2-2 浮點變數 (float) • 語法一 • float a=-3.9; • 語法二 • float b=1.5e2; • 語法三 • float answer; 指數記數法 = 1.5×10²

  33. Ch2_6 浮點變數表示法 • Ch2_6 • #include<stdio.h> • main(){ • float ans; • float a=6.9; • float b=1.2e2; • ans = a*b; • printf("The answer is %f.\n", ans); • } The answer is 828.000000.

  34. 2-2-3 字元變數 (char) Ch2_7 字元變數表示法 1 #include<stdio.h> 2 main(){ 3 char a = 'C'; 4 char x = a-1; 5 printf("x = %c\n", x); 6 } 單引號 'C'-1 = 'B' x = B

  35. 2-2-4 字串變數 (char string[ ]) • 字串變數會隨著程式變化而改變。 Ch2_8 字串變數表示法 1 #include<stdio.h> 2 main(){ 4 char s1[30] = "Taiwan "; 5 char s2[11] = "University"; 6 strcat(s1, s2); // 結合字串 7 printf("%s\n", s1); 8 } 雙引號 Taiwan University

  36. 2-3 資料型態(Data Types) • 整數型態(int type) • 浮點數型態(floating-point type) • 單精確度浮點數型態(float type) • 倍精確度浮點數型態(double type) • 字元型態(char type)

  37. 2-3-1 整數型態(int type)

  38. Ch2_9 sizeof() 佔多少位元 • Ch2_9 整數型態─計算所佔的記憶體空間 • #include<stdio.h> • main(){ • printf("The size of int is %i bytes.\n", sizeof(int)); • printf("The size of short int is %i bytes.\n", sizeof(short int)); • printf("The size of long int is %i bytes.\n", sizeof(long int)); • } The size of int is 2 bytes. The size of short int is 2 bytes. The size of long int is 4 bytes.

  39. 2-3-2 浮點數型態(floating-point type)

  40. Ch2_10 • Ch2_10 浮點數型態─計算所佔的記憶體空間 • 1 #include<stdio.h> • 2 main() { • 3 printf("The size of float is %i bytes.\n", sizeof(float)); • 4 printf("The size of double is %i bytes.\n", sizeof(double)); • 5 printf("The size of long double is %i bytes.\n", • sizeof(long double)); • 6 } 佔多少位元 The size of float is 4 bytes. The size of double is 8 bytes. The size of long double is 10 bytes.

  41. 浮點數型態 比較有效位數的不同 1 #include<stdio.h> 2 main(){ 3 float f= 5/3.0; 4 double d= 5/3.0; 5 printf("f = %20.18f\n", f); 6 printf("d = %20.18f\n", d); 7 } f = 1.666666626930236820 d = 1.666666666666666740 

  42. 2-3-3 字元型態(char type)

  43. Ch2_11 字元型態 • Ch2_11 ─字元與ASCII Code值的轉換 • #include<stdio.h> • main(){ • char x = 'A', y = 'a', z = '0'; • printf("The ASCII code of %c is %i.\n", x, x); • printf("The ASCII code of %c is %i.\n", y, y); • printf("The ASCII code of %c is %i.\n", z, z); • } %c字元 %i數字 The ASCII code of A is 65. The ASCII code of a is 97. The ASCII code of 0 is 48.

  44. 第三章 基本輸入輸出函數 3-1 輸出函數 printf, puts,putchar, putch, putc 3-2 輸入函數 scanf, gets, getc, getch, getche, getchar

  45. 3-1 輸出函數

  46. 3-1-1 輸出 printf • 語法一 • printf("普通字元") ; Ch3_1 printf()輸出 1 #include<stdio.h> 2 main(){ 3 printf("Taiwan University.\n"); 4 } Taiwan University.

  47. 3-1-1 printf()格式化輸出 • 語法二 • printf("普通字元 %? \n", 變數) ; Ch3_2 1 #include<stdio.h> 2 main(){ 3 int a=9, b=6, answer; 4 answer = a-b; 5 printf("The answer:%i-%i = %i.\n",a, b, answer); 6 } The answer:9-6 = 3.

  48. 3-1-2 影響數值輸出的因子 • 資料轉換型態 • 資料的欄寬和精確度 • 帶有\的字元常數 • ASCII字元輸出

  49. printf()函數資料轉換型態

  50. Ch3_3 八進位、十六進位 將十進位的數值轉換成字元(%c)、八進位(%o)、十六進位(%x) 1 #include<stdio.h> 2 main(){ 3 int a = 69; The ASCII Code of 69 is E. The Octal value of 69 is 105. The Hexadecimal value of 69 is 45. 4 printf("The ASCII Code of %i is %c.\n", a, a); 5 printf("The Octal value of %i is %o.\n", a, a); 6 printf("The Hexadecimal value of %i is %x.\n", a, a); 7 }

More Related