當前位置:首頁 » 編程語言 » c語言輸入回車怎麼辦
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言輸入回車怎麼辦

發布時間: 2022-07-09 17:30:42

c語言怎麼解決scanf()把回車作為輸入值的問題

scanf()是不會把回車拷貝到字元竄裡面的。

這里是一段英文定義:the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- seeisspace).

除了回車,空格也不會拷貝到字元竄中。


再來看下gets(),英文定義是這樣的:Reads characters from thestandard input(stdin) and stores them as a C string intostruntil a newline character or theend-of-fileis reached.The newline character, if found, is not copied intostr.

好了。這里寫的很清楚了。gets()雖然可以把輸入都拷貝到字元竄里,比如空格,但是不包含回車。


如果需要回車,可以用fgets()。英文是這樣定義的:Reads characters fromstreamand stores them as a C string intostruntil (num-1) characters have been read or either a newline or theend-of-fileis reached, whichever happens first. A newline character makesfgetsstop reading, but it is considered a valid character by the function and included in the string copied tostr.

最後一句話說了,回車作為結束,不過會拷貝到字元竄中。


那麼文檔說的對不對呢?寫一段代碼測試一下。

#include<stdio.h>

intmain()
{
inti=0;

printf("scanf... ");
charscanf_content[256]={0};
scanf("%s",scanf_content);
printf("value:%s ",scanf_content);
while(scanf_content[i])
{
if(scanf_content[i]==' ')
printf("\n");
else
printf("%d ",(int)scanf_content[i]);

++i;
}

i=0;
printf("gets... ");
chargets_content[256]={0};
gets(gets_content);//unsafe
printf("value:%s ",gets_content);
while(gets_content[i])
{
if(gets_content[i]==' ')
printf("\n");
else
printf("%d ",(int)gets_content[i]);

++i;
}

i=0;
printf("fgets... ");
charfgets_content[256]={0};
fgets(fgets_content,256,stdin);
printf("value:%s ",fgets_content);

while(fgets_content[i])
{
if(fgets_content[i]==' ')
printf("\n");
else
printf("%d ",(int)fgets_content[i]);

++i;
}

return0;
}


輸入「123 123」,你會發現scanf只會得到123,而gets可以得到空格123。最後fgets可以得到' '。這里為了看到空格和回車,可以把字元竄轉成int列印出來。


最後的結論就是,如果需要回車,就使用fgets。

② c語言如何做到輸入回車換行而不是輸出結果

拍入Enter健時, c語言 通常 略去 回車,而只取用 換行鍵。
一定要輸入 回車,你可以用輸入 ASCII 值 13 代替。
例如,你拍入數值13和Enter健,用下面程序,則 s[0] 讀到回車,s[1]讀到換行 :
char s[10];
scanf("%d",&s[0]);
s[1]=getchar();

printf("%c %c\n",s[0],s[1]);
printf("%02x %02x",s[0],s[1]); // 輸出它們的16進制ASCII碼值 0d 0a

③ C語言如何做到回車停止輸入

1、打開軟體,直接使用int類型來定義一個變數用於保存getchar()返回的字元類型。

④ c語言如何做到輸入回車換行而不是輸出結果

代碼可以這樣寫:

#include <stdio.h>

int main()

{

char s[2][128];

int i,a,b,c,d;

for(i=0;i<2;i++)

{

scanf("%d%d%d%d",&a,&b,&c,&d);

sprintf(s[i],"%d+%d+%d+%d=%d",a,b,c,d,a+b+c+d);

}

for(i=0;i<2;i++)

printf("%s ",s[i]);

return 0;

}

這是運行截圖:

⑤ c語言回車是什麼字元

回車符(carriage return,』 』)。

例:

int main()

{

char ch;

ch = getchar();

printf("%d ", ch);

}

輸出結果:

(5)c語言輸入回車怎麼辦擴展閱讀:

注意事項

在Windows系統中回車鍵被當做 的組合來使用,當從鍵盤輸入回車鍵時,Windows系統會把回車鍵當做 來處理(只不過上面的四種字元輸入函數讀取的結果不同)。

getchar——換行符' '(ASCII值為10)

getch——回車符' '(ASCII值為13)

getche——回車符' '(ASCII值為13)

scanf——換行符' '(ASCII值為10)

回車:使游標移到行首

換行:使游標移到下一行

⑥ c語言怎麼解決scanf()把回車作為輸入值的問題,請仔細看我的代碼

你的問題在於空格。
如果scanf裡面有空格(你當前代碼),那麼輸入也要加上空格:6
+
5回車
如果按照你的輸入,那麼scanf裡面格式化字元串之間的空格要去掉。

⑦ 請問 C語言中回車鍵應該怎樣輸入

如果要在程序中表示回車鍵,只要用轉義字元'\n'就可以了。
例如,執行輸出語句
printf("Hello!\nToday
is
Friday!\n");
後,就可以得到二行內容:
Hello!
Today
is
Friday!