Getting multiple values with scanf()

Viewed 269717

I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by doing:

scanf( "%i", &minx);

But I would like the user to be able to do something like:

Enter Four Ints: 123 234 345 456

Is it possible to do this?

8 Answers

Just to add, we can use array as well:

int i, array[4];
printf("Enter Four Ints: ");
for(i=0; i<4; i++) {
    scanf("%d", &array[i]);
}

The question is old, but if someone could help from this with real-life example.

For Single Input data -

int number;
printf("Please enter number : ");
scanf("%d", &number);

For Multiple Input data at a line -

int number1, number2;
printf("Please enter numbers one by one : ");
scanf("%d %d", &number1, &number2);
  • %d %d is for decimal format. You could use format which is suitable for your data type as many as needs with just a space
  • &number1, &number2 - Use comma between variable names

If needs More real-life example check this practical example - https://devsenv.com/tutorials/how-to-take-input-and-output-in-c-programming

Hope, this will help someone.

int a[1000] ;
for(int i = 0 ; i <= 3 , i++)
scanf("%d" , &a[i]) ;
Related