Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting multiple values with scanf()

Tags:

c

scanf

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?

like image 927
Josh Curren Avatar asked Sep 11 '09 18:09

Josh Curren


People also ask

Can scanf take multiple inputs?

Inputting Multiple ValuesIf you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables.

How do I scan more than one value in a scanf?

scanf() reads data from the keyboard. To read in more than one value just use the two individual formats separated by spaces such as "%d %d".

How do you accept multiple inputs on the same input line?

# Taking multiple inputs in a single line. # and type casting using list() function. x = list(map(int, input("Enter multiple values: "). split()))

How do you read multiple strings in one line?

To read multiple string values from a single line entered by user in a specified format via standard input in C language, use scanf() function and pass the format and variables as arguments. scanf() reads input from stdin(standard input), according to the given format and stores the data in the given arguments.


1 Answers

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

scanf( "%i %i %i %i", &minx, &maxx, &miny, &maxy); 
like image 168
Reed Copsey Avatar answered Sep 20 '22 06:09

Reed Copsey