1 / 28

基本 C 語言教學

基本 C 語言教學. 參考網站 h ttp://imil.au.edu.tw/~hsichcl/C_OnLine.htm. 標 準 C 語 言 格 式. #include &lt; stdio.h &gt; ----&gt; 前置處理器 main( ) ----&gt; 主程式 { int i=0 ; /* 變 數 宣 告 * / i=i+1; /* 算 數 運 算 * /----&gt; 程式主體 printf(“i=%d<br>”,i) ; /* 輸 出 至 螢 幕 * / } subroutine1( ) ----&gt; 副程式 {

huey
Download Presentation

基本 C 語言教學

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 語言教學 參考網站http://imil.au.edu.tw/~hsichcl/C_OnLine.htm

  2. 標 準 C 語 言 格 式 • #include < stdio.h > ----> 前置處理器main( ) ----> 主程式{ int i=0 ; /* 變 數 宣 告 */ i=i+1; /* 算 數 運 算 */----> 程式主體printf(“i=%d\n”,i) ; /* 輸 出 至 螢 幕 */ }subroutine1( ) ----> 副程式{ .. } subroutine2( ) ----> 副程式{ .. }

  3. 資 料 型 態 種 類

  4. 變 數 的 宣 告 方 法 • ( 1 ) 整 數 部 份 :int a; int b, c=45; short int name;long int var_name;unsigned int argu1=30; • ( 2 ) 浮 點 數 部 份 :   float a=12.3456;  /*單精準度*/ float b=0.12345e2;double score=4.987654322e-7;  /*雙精準度*/ • ( 3 ) 字 元 部 份 : • char c= 'c';char bell=7;  /*7為ASCII之響聲字元*/

  5. 局部(local)和整體(Global) 變數 #include <stdio.h> int step = 10; /* 整體變數宣告 */ int count = 5; /* 整體變數宣告 */  void main () { int step = 0; /* 局部變數 step 宣告 */ }

  6. 格 式 化 輸 出 函 數 printf( )

  7. 運算子 • 加 ( + )、減 ( - )、乘 ( * )、除 ( / ) 為 一 般 的 四 則 運 算 , 而 ( % )運 算 是 經 由 兩 常 數 相 除 所 得 的 餘 數

  8. 運算優先權 • 括號()具有最高優先權

  9. If(判斷式) • if (關係運算元) • { statement 1 ; statement 2 ; • statement n ; }else { statement 1 ; statement 2 ; statement n ; }

  10. Example Void main () { int a,b=2; if (b==2) a=1; else a = 3; ptinrf(“a = %d”,a); } 執行結果: a= 1

  11. While(判斷式) while ( 關係運算元 ) { statement 1 ; statement 2 ; statement n ; }

  12. Example void main () { int a = 0,b = 0; While (b<5) { b=b+1 } printf(“b = %d”,b) } 執行結果:b = 4

  13. for ( 初始值;判斷式;迴圈執行完之動作) for ( 計數器初值,關係運算元,計數器值更新 ) { statement 1 ; statement n ; }  

  14. Example int a,b=0; for(a=0;a<4;a++) { b = a+ b; } printf(“b = %d”,b); 執行結果:b = 6;

  15. switch (變數) switch ( 變數) { case 常數 1 : statement 1 ; case 常數 2 : statement 2; default: statement n ; }

  16. Example int var=2; switch ( var ) { case 1 : printf("var = 1\n"); break; case 2 : printf("var = 2\n"); break; default : printf("I'm in default"); } /* end of switch */ var = 2

  17. 何 謂 函 數 ? • 1.      C語言使用的函數可以寫出非常漂亮的程式結構,使程式簡單化,偵錯容易。 • 2.      將重複之某些指令撰寫成一個函數,可減少編輯程式時間,更可使程式精簡,清晰了解。 • 3.      C語言使用的函數其呼叫方法與數學上使用函數完全相同,

  18. 回傳之資料型別函數名稱(引數)  { 函數的本體(執行敘述) }

  19. Example Line (8); -------------------------------- voidLine (int j) {   printf(“%d\n”,j); } 執行結果: 8

  20. Example 2 int a; a = Line (8); printf(“%d\n”,a); -------------------------------- int Line (int j) {   j = j+9; return (j); } 執行結果: 17

  21. i=5   if i>5:        print "i大於5"   elif i==5:        print "i等於5"   else:        print "i小於5" 

  22. 陣 列 • 一 維 陣 列 的 宣 告 方 式 如 下 所 述 : • 陣列的資料型態陣列的名稱[ 陣列的大小] ;  intarray1 [ 20 ] ; floatarray2 [ 25 ] ; char array3 [ 50 ] ; Ex : int score[5]={ 2, 5, 7 ,9 ,10 }; score[0]=2 score[1]=5 score[2]=7 score[3]=9 score[4]=10

  23. 二維 陣 列 的 宣 告 方 式 如 下 所 述 : • 二陣列的資料型態陣列的名稱[ 列陣列的大小][ 行陣列大小] ; • intarray1 [ 10 ][ 20 ] ; • floatarray2 [ 5 ][ 25 ] ; • char array3 [ 100 ][ 50 ] ;

  24. 二維 陣 列 的 宣 告範例 • int score1[2][5]={{ 1, 2, 3, 4, 5 },{ 5, 4, 3, 2, 1 }}; • --------------------------------------------------------------- • void main(void) • { • int score1[2][5]; /* 陣列宣告 */ • score1[0][0]=1; /* 程式本體 */ • score1[0][1]=2; score1[0][2]=3; score1[0][3]=4; score1[0][4]=5; score1[1][0]=5; score1[1][1]=4; score1[1][2]=3; score1[1][3]=2; score1[1][4]=1; …… }

  25. 指 標 的 基 本 觀 念 <1>在 C 語 言 中 , 若 某 變 數 所 含 的 是 一 個 記 憶 體 位址,此變 數 稱 為 指 標 變 數。如 下 圖 所 示 <2> 在 C 語 言中 , 指 標 變 數 的 宣 告 方 式 如 下 : 變數資料型態*變數名稱; For example int *ptr ;

  26. Example

  27. Example

More Related