c语言如何输入一串数字,每个数字后面有一个逗号,且不确定输入多少个数字?

例如,输入 3,1,12,4,55,
最后一个数后面也有逗号。
怎样把各个数字赋值到数组中,或者是变量中?

先读1个整型数。然后循环:读1个字符,如果字符是逗号则读1个整型数,如果不是逗号,循环就结束。如果读整型数有错,循环也结束。 #include #include main(){ int x[100],n=0,i; int c; if ( scanf("%d",&x[n])==1) n++; while(1){ scanf("%c",&c); if (c != ',') break; if ( scanf("%d",&x[n])==1) n++;else break; } printf("\nI read: "); for (i=0;i
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答