15

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()) != '\n')
{
    if (c != ' ')
        arr[i++] = c - '0'; 
}

but, of course, this stores one digit per element.

If the user was to type:

10 567 92 3

I was wanting the value 10 to be stored in arr[0], and then 567 in arr[1] etc.

Should I be using scanf instead somehow?

3 Answers 3

37

There are several approaches, depending on how robust you want the code to be.

The most straightforward is to use scanf with the %d conversion specifier:

while (scanf("%d", &a[i++]) == 1)
  /* empty loop */ ;

The %d conversion specifier tells scanf to skip over any leading whitespace and read up to the next non-digit character. The return value is the number of successful conversions and assignments. Since we're reading a single integer value, the return value should be 1 on success.

As written, this has a number of pitfalls. First, suppose your user enters more numbers than your array is sized to hold; if you're lucky you'll get an access violation immediately. If you're not, you'll wind up clobbering something important that will cause problems later (buffer overflows are a common malware exploit).

So you at least want to add code to make sure you don't go past the end of your array:

while (i < ARRAY_SIZE && scanf("%d", &a[i++]) == 1)
  /* empty loop */;

Good so far. But now suppose your user fatfingers a non-numeric character in their input, like 12 3r5 67. As written, the loop will assign 12 to a[0], 3 to a[1], then it will see the r in the input stream, return 0 and exit without saving anything to a[2]. Here's where a subtle bug creeps in -- even though nothing gets assigned to a[2], the expression i++ still gets evaluated, so you'll think you assigned something to a[2] even though it contains a garbage value. So you might want to hold off on incrementing i until you know you had a successful read:

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

Ideally, you'd like to reject 3r5 altogether. We can read the character immediately following the number and make sure it's whitespace; if it's not, we reject the input:

#include <ctype.h>
...
int tmp;
char follow;
int count;
...
while (i < ARRAY_SIZE && (count = scanf("%d%c", &tmp, &follow)) > 0)
{
  if (count == 2 && isspace(follow) || count == 1)
  {
    a[i++] = tmp;
  }
  else
  {
    printf ("Bad character detected: %c\n", follow);
    break;
  }
}

If we get two successful conversions, we make sure follow is a whitespace character - if it isn't, we print an error and exit the loop. If we get 1 successful conversion, that means there were no characters following the input number (meaning we hit EOF after the numeric input).

Alternately, we can read each input value as text and use strtol to do the conversion, which also allows you to catch the same kind of problem (my preferred method):

#include <ctype.h>
#include <stdlib.h>
...
char buf[INT_DIGITS + 3]; // account for sign character, newline, and 0 terminator
...
while(i < ARRAY_SIZE && fgets(buf, sizeof buf, stdin) != NULL)
{
  char *follow; // note that follow is a pointer to char in this case
  int val = (int) strtol(buf, &follow, 10);
  if (isspace(*follow) || *follow == 0)
  {
    a[i++] = val;
  }
  else
  {
    printf("%s is not a valid integer string; exiting...\n", buf);
    break;
  }
}

BUT WAIT THERE'S MORE!

Suppose your user is one of those twisted QA types who likes to throw obnoxious input at your code "just to see what happens" and enters a number like 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 which is obviously too large to fit into any of the standard integer types. Believe it or not, scanf("%d", &val) will not yak on this, and will wind up storing something to val, but again it's an input you'd probably like to reject outright.

If you only allow one value per line, this becomes relatively easy to guard against; fgets will store a newline character in the target buffer if there's room, so if we don't see a newline character in the input buffer then the user typed something that's longer than we're prepared to handle:

#include <string.h>
...
while (i < ARRAY_SIZE && fgets(buf, sizeof buf, stdin) != NULL)
{
  char *newline = strchr(buf, '\n');
  if (!newline)
  {
    printf("Input value too long\n");
    /**
     * Read until we see a newline or EOF to clear out the input stream
     */
    while (!newline && fgets(buf, sizeof buf, stdin) != NULL)
      newline = strchr(buf, '\n');
    break;
  }
  ...
}

If you want to allow multiple values per line such as '10 20 30', then this gets a bit harder. We could go back to reading individual characters from the input, and doing a sanity check on each (warning, untested):

...
while (i < ARRAY_SIZE)
{
  size_t j = 0;
  int c;

  while (j < sizeof buf - 1 && (c = getchar()) != EOF) && isdigit(c))
    buf[j++] = c;
  buf[j] = 0;

  if (isdigit(c))
  { 
    printf("Input too long to handle\n");
    while ((c = getchar()) != EOF && c != '\n')   // clear out input stream
      /* empty loop */ ;
    break;
  }
  else if (!isspace(c))
  {
    if (isgraph(c)
      printf("Non-digit character %c seen in numeric input\n", c);
    else
      printf("Non-digit character %o seen in numeric input\n", c);

    while ((c = getchar()) != EOF && c != '\n')  // clear out input stream
      /* empty loop */ ;
    break;
  }
  else
    a[i++] = (int) strtol(buffer, NULL, 10); // no need for follow pointer,
                                             // since we've already checked
                                             // for non-digit characters.
}

Welcome to the wonderfully whacked-up world of interactive input in C.

0

Small change to your code: only increment i when you read the space:

while ((c = getchar()) != '\n')
{
    if (c != ' ')
        arr[i] = arr[i] * 10 + c - '0'; 
    else
        i++;
}

Of course, it's better to use scanf:

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

providing that you have enough space in the array. Also, be careful that the while above ends with ;, everything is done inside the loop condition.

As a matter of fact, every return value should be checked.

5
  • There is no irony posting this answer on a website called stackoverflow:). Try to use safe versions of scanf. Mar 7, 2012 at 10:43
  • 3
    Wouldn't the revision just cause the element to be reassigned to the next digit's value?
    – bqui56
    Mar 7, 2012 at 10:44
  • 2
    you probably meant something like: arr[i] = arr[i] * 10 + (c - '0'); (and don't forget to initialize it)
    – Nim
    Mar 7, 2012 at 10:57
  • My bad, fixed the part with arr[i]. On the other hand, using scanf to read digits is safe. Mar 7, 2012 at 14:50
  • What about input of negative numbers. In that case '-' sign and digit will be considered separately. Nov 23, 2013 at 11:19
0

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

#include <stdio.h>

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<n )
   {
      sc += scanf("%d",&arr[i++]);
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.