Presentation is loading. Please wait.

Presentation is loading. Please wait.

第三章 基本的輸出與輸入函數 (Basic Output & Input Function)

Similar presentations


Presentation on theme: "第三章 基本的輸出與輸入函數 (Basic Output & Input Function)"— Presentation transcript:

1 第三章 基本的輸出與輸入函數 (Basic Output & Input Function)
printf 輸出至螢幕 (標準輸出) scanf 從鍵盤輸入 (標準輸入) 數值資料的輸入與輸出 文字資料的輸入與輸出

2 Demo Program 1: Basic Input/output
/* demo1.c : basic I/O for beginner */ /* input last 3 digits of your id , say hello */ #include <stdio.h> main() { int id; printf("Input last 3 digits of your id:"); scanf("%d", &id); printf("Hello ! %d", id); }

3 Demo1.C 執行畫面(User Screen)
Input last 3 digits of your id:123 Hello ! 123

4 Demo Program 2: basic program structure
/* demo2.c : basic program structure */ /* input --> calculate --> output */ #include <stdio.h> main() { int n, square; /* step 1: input data */ printf("Input a number :"); scanf("%d", &n); /* step 2: calculate data */ square = n*n; /* step 3: output data */ printf("The square is %d", square); }

5 Demo2.C 執行畫面(User Screen)
Input a number :12 The square is 144

6 printf 指定格式的輸出函數 程式開頭須加 #include <stdio.h> int a=3,b=4;
例1: printf("%d %d",a,b); 印出 3 4 例2: printf("a=%d b=%d",a,b); 印出 a=3 b=4 例3: printf("a=%d\nb=%d",a,b); 印出 a=3 b=4

7 整數格式的輸出 %d %d 十進位整數 例如 int a=27; printf("[%d]",a); 會印出 [27]

8 整數格式的輸出 %nd %-nd %nd 十進位整數, 占 n 格位置(靠右) 例如 int a=123; printf("[%6d]",a); 會印出 [● ● ● 123] 其●表示空白 %-nd 十進位整數, 占 n 格位置(靠左) printf("[%-6d]",a); 會印出 [123● ● ● ] 其●表示空白

9 整數格式的輸出 %x %x 十六進位整數 例如 int a=27; printf("[%x]",a); 會印出 [1b]

10 浮點數格式的輸出 %f %f 浮點數格式 例如 float pi= ; printf("[%f]",pi); 會印出 [ ] (預設取六位小數)

11 浮點數格式的輸出 %n.mf %n.mf 浮點數格式, 共占 n 格位置 小數點以下取 m 位 例如 float pi= ; printf("[%6.2f]",pi); 會印出 [● ● 3.14] 其●表示空白

12 浮點數格式的輸出 %e %e 指數格式 例如 float a= ; printf("[%e]",a); 會印出 [ e+02]

13 字元格式的輸出 %c %c 字元格式 例如 char ch='A'; printf("%c",ch); 會印出 A 例如 char a='A'; printf("%c %c %d", a, 'a', a); 會印出 A a 65

14 字串格式的輸出 %s %s 字串格式 例如 char *msg="ABC"; printf("%s",msg); 會印出 ABC 例如 char *s="X\'mas"; printf("the word is %s", s); 會印出 the word is X'mas

15 scanf 指定格式的輸入函數 程式開頭須加 #include <stdio.h> int a,b;
例1: scanf("%d", &a); 輸入變數 a 的值 scanf("%d", &b); 輸入變數 b 的值 例2: scanf("%d%d", &a,&b); 輸入變數 a與 b 的值

16 整數格式的輸入 %d %d 十進位整數 例如 int a; printf("input an integer"); scanf("%d", &a); 由鍵盤輸入一個整數值,存入變數a

17 浮點數格式的輸入 %f 例如 float a; printf("input a number"); scanf("%f", &a); 由鍵盤輸入一個浮點數值,存入變數a

18 字元格式的輸入 %c 例如 char ch; printf("input a symbol"); scanf("%c", &ch); 由鍵盤輸入一個字元符號,存入變數ch

19 printf("input a symbol"); ch = getchar();
程式開頭須加 #include <stdio.h> 例如 char ch; printf("input a symbol"); ch = getchar(); 由鍵盤輸入一個字元符號 (按Enter鍵結束),存入變數ch

20 printf("input a symbol"); ch = getche();
程式開頭須加 #include <conio.h> 例如 char ch; printf("input a symbol"); ch = getche(); 由鍵盤直接輸入一個字元符號 (不必按Enter鍵結束),存入變數ch,此字元會顯示在螢幕上

21 printf("input a symbol"); ch = getch();
程式開頭須加 #include <conio.h> 例如 char ch; printf("input a symbol"); ch = getch(); 由鍵盤直接輸入一個字元符號 (不必按Enter鍵結束),存入變數ch,此字元不會顯示在螢幕上

22 程式觀摩【範例 3-4-1】 p1/2 1 /* EX3-4-1 */
運用整數的基本運算符號,計算兩個整數的和、差、乘積、商、餘數。 1 /* EX3-4-1 */ 2 /* simple calculation + - * / % */ 3 補兩行 #include <stdio.h> 與 #include <conio.h> 4 main() 5 { int num1, num2; 7 printf("<< Simple Calculation >>\n"); printf("Input two integer number : "); scanf("%d%d",&num1,&num2); printf("%d + %d = %d\n", num1,num2,num1+num2);

23 程式觀摩【範例 3-4-1】 p2/2 12 printf("%d - %d = %d\n",num1,num2,num1-num2);
14 printf("%d / %d = %d (integer division)\n", num1,num2,num1/num2); 15 printf("%d / %d = %f (real division)\n", num1,num2,(float)num1/num2); 16 /* use %% to print a % symbol */ 17 printf("%d %% %d = %d\n", num1,num2,num1%num2); 18 /* press any key to end the program */ 19 getch(); 20 }

24 程式觀摩【範例 3-4-1】 的輸出畫面 << Simple Calculation >>
Input two integer number : 7 3 7 + 3 = 10 7 - 3 = 4 7 * 3 = 21 7 / 3 = 2 (integer division) 7 / 3 = (real division) 7 % 3 = 1

25 程式觀摩【範例 3-4-2】 p1/2 這是基本的數值運算的運用。本程式分別輸入華氏溫度及攝氏溫度,求出其相對的攝氏及華氏溫度值。
1 /* EX3-4-2 */ 2 /* Fahrenheit/Celsius conversion for temperature */ 3補兩行 #include <stdio.h> 與 #include <conio.h> 4 main() 5 { int fdegree, cdegree; 7 printf("Input temperature degree in Fahrenheit scale : "); scanf("%d",&fdegree); cdegree = (fdegree-32)*5/9; printf("%d degree Fahrenheit == %d degree Celsius \n",fdegree,cdegree);

26 程式觀摩【範例 3-4-2】 p2/2 12 13 printf("Input temperature degree in Celsius
scale : "); scanf("%d",&cdegree); fdegree = cdegree*9/5+32; printf("%d degree Celsius == %d degree Fahrenheit\n",cdegree,fdegree); 17 getch(); 19 }

27 程式觀摩【範例 3-4-2】 的輸出畫面 Input temperature degree in Fahrenheit scale : 78
78 degree Fahrenheit == 25 degree Celsius Input temperature degree in Celsius scale : 100 100 degree Celsius == 212 degree Fahrenheit


Download ppt "第三章 基本的輸出與輸入函數 (Basic Output & Input Function)"

Similar presentations


Ads by Google