Scan multiple integers without knowing the actual number of integers

My advice, go for fgets().

  1. Read the whole line from the input
  2. Tokenize using space [] [or your preferred delimiter] [using strtok()]
  3. Allocate memory to store the integer
  4. Convert the string input to integer [maybe strtol()] and store each integer.

Optionally, you may want to add some validation and error checking.

Read more about fgets() here.

also, don’t forget to get rid of the trailing \n stored in the read buffer by fgets()

Leave a Comment