40

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?

1
  • 1
    @Josh Curren %d converts input as if it is a decimal representation. %i converts input as if it is a decimal, hexadecimal or octal string using the usual leading 0, 0x, 0X to steer toward octal or hexadecimal. Example: "010" converts differently. Commented Nov 15, 2013 at 19:42

8 Answers 8

71

You can do this with a single call, like so:

scanf( "%i %i %i %i", &minx, &maxx, &miny, &maxy);
Sign up to request clarification or add additional context in comments.

5 Comments

%d is almost identical to %i. They do the same thing in most cases.
@Chris: under what circumstance does %i not do what %d does?
@Jonathan Leffer - According to my scanf manpage, "%i" reads the int in base 16 if it begins with 0x and in base 8 if it begins with 0 and base 10 otherwise. "%d" reads in base 10 always. And the Open Group manpage agrees with mine.
@wrang-wrang: Yes, it should. Normally, if you'd use a %d, though, unless you expect non-base 10 numeric data entry.
10

Yes.

int minx, miny, maxx,maxy;
do {
   printf("enter four integers: ");
} while (scanf("%d %d %d %d", &minx, &miny, &maxx, &maxy)!=4);

The loop is just to demonstrate that scanf returns the number of fields succesfully read (or EOF).

2 Comments

The only trouble with that code is that if the user enters just 1 value, then they have to re-enter all 4 values on the next cycle.
This will turn into an infinite loop if I enter invalid data(like characters)
8
int a,b,c,d;
if(scanf("%d %d %d %d",&a,&b,&c,&d) == 4) {
   //read the 4 integers
} else {
   puts("Error. Please supply 4 integers");
}

Comments

4

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]);
}

Comments

1

Could do this, but then the user has to separate the numbers by a space:

#include "stdio.h"

int main()
{
    int minx, x, y, z;

    printf("Enter four ints: ");
    scanf( "%i %i %i %i", &minx, &x, &y, &z);

    printf("You wrote: %i %i %i %i", minx, x, y, z);
}

1 Comment

"user has to separate the numbers by a space" is not so. User input could be "12-34+56-78".
1

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.

Comments

-1

Passable for getting multiple values with scanf()

int r,m,v,i,e,k;

scanf("%d%d%d%d%d%d",&r,&m,&v,&i,&e,&k);

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.