程式設計
description
Transcript of 程式設計
程式設計
基本輸出入方法 (1)
#include <stdio.h>
main(){
printf("hello, world\n");}
Printf() 列印格式化資料到標準輸出裝置 函 數 名 稱 函 數 參 數
(string)
Printf() 列印格式化資料到標準輸出裝置 ( 續 )
printf("Hello, World\n");– 指示電腦做列印動作
列印出括號內字串 (" ")– 整個指令稱為一個 statement
所有指令都要有結束符號 (;)– 跳脫字元 (\)
指示電腦做硬體的控制動作 \n 是跳行的動作
跳脫字元
跳脫字元 說明\n new line 跳行\r Carriage returnEnter 鍵\t Horizontal tab 水平定位\v Vertical tab 垂直定位\” Double quotation 插入雙引號\’ 插入單引號\\ 插入反斜線
printf 列印格式化資料到標準輸出裝置
輸出格式 int printf(“ 格式字串” [,$ 參數 .....);
scanf 從標準輸入裝置讀入格式資料
輸入格式 int scanf(“ 格式字串” [,$ 參數 .....);
基本輸入輸出函數
輸出函數printf, puts,putchar, putch, putc
輸入函數scanf, gets, getc, getch, getche, getchar
函數 輸出
printf() 數值、字元和字串
putc() 字元
putch() 字元
putchar() 字元
puts() 字串
輸出函數
1 #include<stdio.h>2 main(){3 printf("Taiwan University.\n");4 }
Taiwan University.
printf() 輸出 ( 語法一 )
1 #include<stdio.h>2 main(){3 int a=9, b=6, answer;
The answer: 9-6 = 3.
printf() 格式化輸出 ( 語法二 )
4 answer = a-b;5 printf("The answer: %i-%i = %i.\n",
a, b, answer);6 }
• 資料轉換型態• 資料的欄寬和精確度• 帶有 \的字元常數 • ASCII 字元輸出
影響數值輸出的因子
% 字元 功能敘述
%i,%d 輸出十進位的整數
%c 輸出字元
%s 輸出字串
%f 輸出以小數點表示的浮點數
printf() 函數資料轉換型態
%e 輸出以指數表示的浮點數
%g 自動選擇以小數點表示或指數表示的浮點數
%o 輸出八進位的整數
%x 輸出十六進位的整數
八進位、十六進位轉換
The ASCII Code of 69 is E.The Octal value of 69 is 105.The Hexadecimal value of 69 is 45.
1 #include<stdio.h>
2 main(){
3 int a = 69; 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 }
• 語法一• %ni e.g. %6i, %02i
• 語法二• %-nd e.g. %-6d
+ 靠右
- 靠左
前面補 0
顯示資料的欄寬和精確度