1 / 15

輸出與輸入 (I/O)

輸出與輸入 (I/O). 標準輸出與輸入. 標準輸出 (Standard Output, stdout): 將資料送到螢幕顯示 . 標準輸入 ((Standard Input, stdin): 接收鍵盤輸入的資料. 格式化輸出 (printf 函數 ). printf 函數的作用是將欲顯示的訊息輸出至螢幕 , 使用語法如下 : printf( “ 顯示字串 "); 例如 : printf("Every day is working day."); printf("This number is 2."); 執行結果:.

rhoda
Download Presentation

輸出與輸入 (I/O)

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. 輸出與輸入(I/O)

  2. 標準輸出與輸入 • 標準輸出(Standard Output, stdout): 將資料送到螢幕顯示. • 標準輸入((Standard Input, stdin): 接收鍵盤輸入的資料.

  3. 格式化輸出(printf函數) • printf函數的作用是將欲顯示的訊息輸出至螢幕,使用語法如下: printf(“顯示字串"); • 例如 : printf("Every day is working day."); printf("This number is 2."); • 執行結果:

  4. 把變數num的內容以%d的格式替換到這兒 格式化輸出(printf函數)-不同型態資料顯示 • printf函數的作用是將欲顯示的訊息輸出至螢幕,使用語法如下: printf( “格式字串格式碼” ,參數1,參數2, …); • 各種資料型態的控制格式(格式碼)如下頁所示. • 參數列: 欲輸出的資料的變數,常數或運算式 • 例如 : printf(“I have %d cats.”, num); printf(“This number is %d.”,2); printf(“%d”,1/3); 如果num=3,則結果為 I have 3 dogs. 結果為 This is number two: 2. 結果為 0

  5. 課堂練習 • 請使用Dev C++,並將以下的程試碼存檔並執行. • #include<stdio.h> • int main(void) • { • int a=2; • int b=3; • printf("I have %d dogs and %d cats\n", a, b); • system(“pause"); • return 0; • } • 執行結果如下所示: 可使執行結果容易觀看

  6. 格式化的輸入(scanf函數) • scanf函數的作用是將鍵盤輸入的資料,指定為變數的值,使用語法如下: scanf(“格式碼,變數位址1,變數位址2,…) • 例如: • #include<stdio.h> • int main(void) • { • int x,y; • printf("Please input two numbers:\n"); • scanf("%d %d" ,&x, &y); • printf("results:%d, %d",x,y); • system("pause"); • return 0; • } /*兩個控制格式之間用空白隔開,故輸入整數時亦需用空白(或換行或Tab鍵)隔開,按Enter鍵,表示輸入完畢,則系統才會繼續執行.*/ 程式執行到此行,會等待使用者輸入兩個數字

  7. 格式化的輸入(scanf函數) • #include<stdio.h> • int main(void) • { • int x,y; • printf("Please input two numbers:\n"); • scanf("%d,%d",&x,&y); • printf("results:%d,%d",x,y); • system("pause"); • return 0; • }/*若以逗號隔開兩個輸入的格式碼,在輸入資料時,也必須以逗號來區隔*/ • 請試試看若是在輸入第一筆資料後,直接按下”Enter”鍵,會有什麼結果?

  8. printf()與scanf()的控制格式對照表

  9. 輸入字元的函數 字元資料輸入函數功能對照表

  10. 字元輸出的函數 • 字元輸出函數 • putchar() 將指定的字元送到螢幕顯示,此函數屬於標準輸出入函數(stdio.h) 函數格式為 putchar(變數); • putch() 將指定的字元送到螢幕顯示,此函數屬於控制台的輸出入函數(conio.h)函數格式為 putch(變數);

  11. 字元輸出函數----練習 /* 字元輸入與輸出函數的應用 */ • #include <stdio.h> • #include<conio.h> • void main(void) • { • char ch1,ch2,ch3; • printf(“Please enter two characters:\n”); /*請直接輸入兩個字元,輸入完即讀入buffer中*/ • ch1=getche(); • ch2=getche(); • printf("\nThe first character is:\n"); /*顯示第一個字元*/ • putchar(ch1); • printf("\nThe second character is:\n"); /*顯示第二個字元*/ • putchar(ch2); • printf("\nPlease input a character:\n"); /*請再輸入一個字元,需按下enter之後才作讀取動作*/ • ch3=getchar(); • printf("\nThe third character is:\n"); • putchar(ch3); • printf("\n\n\n"); • system("pause"); • }

  12. 基本的輸出與輸入----範例2 /* 輸入一個字元 ,顯示相對應的ASCII碼*/ • #include <stdio.h> • #include <stdlib.h> • int main(void) • { • char ch; /* 宣告一個字元變數 */ • /* 使用字元輸入格式符號輸入字元 */ • printf("Please input a character: "); • scanf("%c",&ch); • /* 顯示輸入的字元與相對應之ASCII碼 */ • printf("ch=%c , ASCII code is %d. \n", ch, ch); • system("pause"); • return 0; /* 程式正常結束 */ • }

  13. 讀取字元的常見錯誤 • #include<stdio.h> • int main(void) • { • int num; • char ch; • printf("Please input a integer:\n"); • scanf("%d",&num); • printf("Please input a character:"); • scanf("%c",&ch); • printf("\nnum=%d,ASCII code of character=%d\n",num,ch); • system("pause"); • return 0; • } 此時ch變數的內容為”enter”(換行字元)而不能再存入使用者要鍵入之字元

  14. 讀取字元的常見錯誤---解決之道 • 1.在scanf();中的%c之前留一個空白就可以跳過此換行字元. • 2.使用fflush(stdio)函數來清除緩衝區的資料. • #include<stdio.h> • int main(void) • { • int num; • char ch; • printf("Please input a integer:\n"); • scanf("%d",&num); • fflush(stdin); • printf("Please input a character:"); • scanf("%c",&ch); • printf("\nnum=%d,ASCII code of character=%d\n",num,ch); • system("pause"); • return 0; • }

  15. 基本的輸出與輸入----範例3 /* 輸入一個字串 */ • #include <stdio.h> • #include <stdlib.h> • int main() • { • char name[10]; /* 宣告字元陣列 */ • /* 使用字串輸入格式符號輸入字串 */ • printf("What's your name: "); • scanf("%s",&name); /*輸入字串,並由字元陣列name所接收*/ • printf("Hi~ %s , How are you ?\n" , name); /*印出字串的內容*/ • system("pause"); • return 0; /* 程式正常結束 */ • }

More Related