請看下面給出的代碼
int main()
{
char str[20] 。
printf("輸入一個字串:")。
scanf("%s", str) 。
printf("String : %s", str) 。
return 0;
輸出將是,
輸入一個字串 : Hello world
字串:Hello
我的問題是,為什么它只接受第一個字作為輸入?
uj5u.com熱心網友回復:
我的問題是,為什么它只把第一個詞作為輸入?
因為這正是檔案中所說的%s指定器應該做的:
任何數量的非空格字符,在發現第一個空格字符時停止。一個終止的空字符會自動添加到存盤序列的末尾。
https://www.cplusplus.com/reference/cstdio/scanf/
uj5u.com熱心網友回復:
你可以使用以下方法
方法1:scanf("%[^
]",str)。
方法2:
fgets(str, MAX_LIMIT, stdin) 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/332270.html
標籤:
