Presentation is loading. Please wait.

Presentation is loading. Please wait.

I/O of C and C++. printf and scanf of C Should add printf (“control string”, exp1, exp2,…) Scanf(“control string”, &var1, &var2,…)

Similar presentations


Presentation on theme: "I/O of C and C++. printf and scanf of C Should add printf (“control string”, exp1, exp2,…) Scanf(“control string”, &var1, &var2,…)"— Presentation transcript:

1 I/O of C and C++

2 printf and scanf of C Should add printf (“control string”, exp1, exp2,…) Scanf(“control string”, &var1, &var2,…)

3 MinGW C/C++ types Type# of bytesrange int4-2147483648~2147483647 long (int)4-2147483648~2147483647 unsigned (int)40~4294967295 short (int)2-32768~32767 unsiged short (int) 20~65535 char10~255 float41.2e-38~3.4e38 double82.2e-308~1.8e308

4 printf format formatdescription %c 字元 %d 十進位整數 %i 十進位整數 %e 浮點數, e 指數型式 %E 浮點數, E 指數型式 %f 小數點位數 6 位之浮點數 %g 印出 %f 與 %e 較短者 %G 印出 %f 與 %E 較短者 %o 無號八進位整數

5 printf format formatdescription % 印出百分比符號 %p 以十六進位印出指標位址 %s 印出字串 %u 無號十進位數字 %x 無號十六進位整數,以 0-f 表示 %X 無號十六進位整數,以 0-F 表示 %l long ,加在 d,o,u,x,X 之前

6 printf format modifierdescription - 向左齊對,如 %-4f + 將數值的正負符號顯示出來,如 %+f 空白數值為正數時留一空白,為負時顯示負 號,如 % 7.2f 0 將固定欄位長度的數值前補 0 ,與 - 同用 時無效,如 %09f + 將數值的正負符號顯示出來,如 %+f 空白數值為正數時留一空白,為負時顯示負 號,如 % 7.2f

7 scanf format formatdescription %c 輸入字元 %d or %i 輸入整數 %D 輸入長整數 %f or %e %E %G %g 輸入浮點數 %F 輸入倍精準度長整數 %X or %x 輸入無號十六進位整數 例如: int i; scanf(“%x”, &i); 上例中表示以 16 進位讀入整數 i

8 字元字串讀取輸出函數 getchar(): 讀取字元,需按 Enter ,殘留換行字元, 屬於 getche(): 讀取字元,不需按 Enter ,屬於 getch(): 讀取字元,不需按 Enter ,不顯示字元, 屬於 gets(): 讀取字串 ( 包含所有空白 ) 直到按 Enter 為止, 屬於 (scanf(“%s”,chars); 則由第一個非空 白讀到空白為止 ) putchar(): 輸出單一字元,屬於 puts(): 輸出字串,屬於

9 C++ I/O Should add #include cout: console output cin: console input cerr: console error output (no buffer) clog: console buffered output E.G.: redirection will not affect cerr $MyProgram>data

10 C++ I/O manipulator Should add #include dec:10 進位輸出 ( 預設 ) hex:16 進位輸出 oct: 8 進位輸出 endl: ‘\n’ ends: ‘\0’ flush: 將緩衝區資料全部送出 E.G.: cout<<“Dec:”<<100<<“,”<<200<<endl <<“Hex:”<<hex<<100<<“,”<<200<<endl <<“Oct:”<<oct<<100<<“,”<<200<<endl

11 C++ I/O manipulator setbase(int n): 設基底 –n=0 default (10 進位 ) –n=8 (8 進位 ) –n=10 (10 進位 ) –n=16 (16 進位 ) setw(int w): 設寬度 E.G.: int size=20; char a[size]; cin>>setw(size)>>a;

12 C++ I/O manipulator setfill(int c): 設填充字元 E.G.: cout<<“1234567890”<<endl; cout<<setw(10)<<setfill(‘*’)<<“abc”; Outcome: 1234567890 *******abc

13 C++ I/O manipulator setprecision(int n): 設定浮點數精準度位數, 預設 6 位 E.G.: cout<<1.234<<“,”<<setprecision(2)<<1.23 4 <<“,”<<3.456; Outcome: 1.234,1.2,3.5

14 C++ I/O manipulator fill(char): 設定填充字元,並回傳先前填充字元 fill(): 回傳先前填充字元 precision(int) precision() width(int) width() E.G.: cout.width(10); cout.fill(‘*’); cout<<“Good”; cout<<cout.fill(); Outcome: ******Good*

15 C++ I/O manipulator write(const char *str, int len): 輸出字串 put(char): 輸出一字元 E.G.: char s[]="123456789"; s[4]=0; cout<<s<<endl; cout.write(s,8)<<endl; cout.put('a').put('b').write("abcdefg", 4)<<endl; Outcome: 1234 1234 678 ababcd


Download ppt "I/O of C and C++. printf and scanf of C Should add printf (“control string”, exp1, exp2,…) Scanf(“control string”, &var1, &var2,…)"

Similar presentations


Ads by Google