[英]character/string input while debugging using gdb
您的scanf("%d",...)
在緩沖區中保留了一個新行字符,隨后該行字符立即被后續的scanf("%c",...)
。 為了克服這個問題,在scanf("%d",...)
消耗空格后,只允許一個scanf
:
int main()
{
int n,i=0;
scanf("%d",&n);
int a[20];
char c=0;
scanf(" %c",&c); // Consume white spaces including new line character before the value for c.
while(c!='\n' && i < 20)
{
if(c!=32) {
a[i++]=c-'0';
}
scanf("%c",&c);
}
for(int x=0;x<i;x++)
printf("%d ",a[x]);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.