scanfの戻り値を調べてみた_その2

scanf関数の戻り値について若干追記します。

調べたところによると
scanf関数の戻り値は、正常に取得できたデータの個数だそうです。
ですので以下のプログラムでは、a,b,cに正しく整数値を格納できたときのscanfの戻り値は3になります。
異常が生じたときは、正常な個数だけ値が戻されます。

/* three.c */
#include


int main(void)
{
int a,b,c;
int x;

x = scanf("%d%d%d", &a,&b,&c);
printf("Your data is : %d %d %d\n", a,b,c);
printf("Scanf returned %d\n", x);

return 0;
}


$ gcc -o three three.c


$ ./three
1
2
6
Your data is : 1 2 6
Scanf returned 3
$ ./three
3
5
q
Your data is : 3 5 15552500
Scanf returned 2
$ ./three
2
q
Your data is : 2 134513824 4448244
Scanf returned 1
$ ./three
1
q
Your data is : 1 134513824 2527220
Scanf returned 1
$ ./three #EOFを入力してみる
Your data is : -1076778936 134513824 3690484
Scanf returned -1