Putting numbers separated by a space into an array

前端 未结 3 2007
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 22:43

I want to have a user enter numbers separated by a space and then store each value as an element of an array. Currently I have:

while ((c = getchar()) != \'\         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 23:39

    scanf returns the number of items successfully scanned. Give this code a try:

    #include 
    
    int main()
    {
      int arr[500];
      int i = 0;
      int sc = 0; //scanned items
      int n = 3; // no of integers to be scanned from the single line in stdin
    
       while( sc

提交回复
热议问题