3

Is there a way to input n of inputs into an array, where n is not fixed, using scanf()?

scanf("%d %d %d %d ... n number of inputs", &array);

The problem is that a user enters the size of an array, and the input format is that the inputs are stored using scanf in a single line, so it is of the form

12 24 36 34 65 24 54 ... upto n inputs

So that the first %d is stored in array[0], the second into array[1], third into array[2], and all the way upto array[n-1].

It is easy to make a for loop for this, but I want to do it in a single line.

dtell
  • 2,488
  • 1
  • 14
  • 29
Saif Ul Islam
  • 345
  • 3
  • 14

1 Answers1

1

Not one-liner but still short

while(i < n && scanf("%d", &array[i]) == 1)
    i++;

You should always check the return value of scanf function.
Don't forget to initialize i with 0

suvojit_007
  • 1,690
  • 2
  • 17
  • 23