Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次のCコードでは:
char name[20]; int a; int b; for(i=0;i<10;i++) { printf("\nEnter name, a & b: "); scanf("%s %d %d",name,&a,&b); }
2回目の反復入力について、1回目の反復の最後に入力されたものscanfを読み込みますか?'\n'scanf()
scanf
'\n'
scanf()
scanf入力ストリームを。に向けたままにし\nます。あなたの場合、違いはありません。呼び出されるたびにscanf、次の空白以外の文字が見つかるまで移動します。したがってname, a, b、入力として10行を指定すると、期待どおりに機能します。
\n
name, a, b
しかし、これを考慮してください:
scanf("%d", &a); fgets(str, 20, stdin);
fgets最初の改行文字が見つかるまで読み取ります。したがってstr、値を取得するだけで\n、次の入力行は読み取りfgetsません。
fgets
str