たとえば、以下のコードを見つけてください
main()
{
int i;
char s[100];
printf("Enter the string \n");
scanf(" %s ",s);
printf("Enter the string\n");
scanf("%d",&i);
printf("%s\n%d\n",s,i);
}
上記のコードの出力は次のとおりです。
Enter the string
hai
hai
Enter the string
hai
0
1行の入力を受け入れるはずですが、2行目も受け入れています。
scanfのスペースを削除すると、出力は正しく行われます。
誰か説明できますか?
整数(%d)で同じものを使用する場合、それは起こりません。それは文字列で起こっています。