輸入輸出字元和字串總結

2021-10-05 11:16:48 字數 1738 閱讀 6153

scanf輸入單個字元:

char letter;

scanf("%c",&letter);

scanf輸入字串:

給數值型陣列賦值時,只能用for迴圈乙個乙個的賦值,但是字元陣列賦值時可以直接賦值,不需要使用迴圈,系統會在最後新增結束標誌符『\0』.

char a[30];

scanf("%s",a); //可以有&也可以沒有&

#includeint main (void)
輸入:hello world

輸出:hello

#includeint main (void)
輸入:hello world

輸出:hello world

char s1 = "array";

char s2[6] = "array"; //陣列長度,末尾會自動添『0』

printf("%s %c",s1,s2[2]); //輸出 array,r

#includeint main()
#include#includeint main()
示例:ac:a

getche()位於標頭檔案中,只能在windows下使用。

#include#includeint main()
輸入a後,會立即讀取,由於沒有回顯,所以直接輸出內容為c:a

同樣,getch()位於標頭檔案中,只能在windows下使用。

#includeint main()
示例:

we like hoilday

we like hoilday

get()和scanf的主要區別是:

get()認為空格也是字串的一部分,只有遇見回車才認為字串輸入結束。所以,get()能讀取帶有空格的字串,但是scanf()不能。

putchar()其輸出可以是乙個字元(用單引號引起來的),可以是0-127之間的乙個整型數,也可以是char定義好的乙個字元型常量。注意:當c為乙個介於0~127(包括0及127)之間的十進位制整型數時,它會被視為對應字元的ascii**,輸出該ascii**對應的字元。

該函式以無符號 char 強制轉換為 int 的形式返回寫入的字元。

(1)當輸出正確的時候,返回輸出字元轉換為的unsigned int 值;

(2)當輸出錯誤的時候,返回 eof(end of file)檔案結束符

示例:

#includeint main()
輸出:

pwtpwt

puts函式只能用來輸出字串,沒有格式控制,裡面可以是字串變數(字串陣列名或字串指標),執行更快。

相當於printf("%s\n",s);

#includeint main(void)

輸出:hello world

printf輸出格式有很多,不單單是字串,這裡主要講字串。

#includeint main(void)

輸出:hello world

字串和輸入輸出

一.示例程式 include include define density 62.4 int main 該程式的新特性 1.使用乙個陣列來存放字串,可以存放40個位元組,每個位元組都可以存放乙個字元值。2.使用 s轉換說明符來處理字串輸入和輸出。3.使用預處理器定義 4.使用strlen 這個函式來...

字串的輸入 輸出總結(陣列字串)

char str maxn maxn為常量char型別陣列宣告字串必須給出其大小,之後大小固定。cin和scanf 都是遇到 空格 tab 回車 結束,即一次讀取乙個單詞。include scanf s str include cin str cin.get str size 遇到回車或讀滿 siz...

c c 字元 字串輸入輸出

1.scanf 1.1 輸入字元 char ch scanf c ch 注意字元前面的取位址符 1.2 輸入字串 char str 15 scanf s str char pstr scanf s pstr 1 scanf 在輸入字串時,不讀入空格和回車,在讀入空格或回車時,輸入結束 2 輸入字串長...