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?

Josh Curren
  • 10,171
  • 17
  • 62
  • 73
  • 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. – chux - Reinstate Monica Nov 15 '13 at 19:42

8 Answers8

67

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

scanf( "%i %i %i %i", &minx, &maxx, &miny, &maxy);
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • you can use %d %i %o %x %X for ints.. http://montcs.bloomu.edu/~bobmon/Information/LowLevel-Programming/printf-formatting.shtml – Josh Curren Sep 11 '09 at 18:25
  • %d is almost identical to %i. They do the same thing in most cases. – Chris Lutz Sep 11 '09 at 18:26
  • @Chris: under what circumstance does %i not do what %d does? – Jonathan Leffler Sep 11 '09 at 18:32
  • 8
    @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. – Chris Lutz Sep 11 '09 at 18:35
  • @wrang-wrang: Yes, it should. Normally, if you'd use a %d, though, unless you expect non-base 10 numeric data entry. – Reed Copsey Sep 11 '09 at 22:20
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).

Jonathan Graehl
  • 9,182
  • 36
  • 40
6
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");
}
nos
  • 223,662
  • 58
  • 417
  • 506
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]);
}
fcdt
  • 2,371
  • 5
  • 14
  • 26
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);
}
l3dx
  • 2,930
  • 6
  • 33
  • 43
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.

Maniruzzaman Akash
  • 4,610
  • 1
  • 37
  • 34
-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);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-2
int a[1000] ;
for(int i = 0 ; i <= 3 , i++)
scanf("%d" , &a[i]) ;
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
saba razi
  • 1
  • 1