Getting multiple values with scanf() - c

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?

You can do this with a single call, like so:
scanf( "%i %i %i %i", &minx, &maxx, &miny, &maxy);

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).

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

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

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

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.

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

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

Related

How to prompt user to enter multiple values on the same line?

How can we have a user enter three different values on the same line.
Something like this: enter three numbers: 1, 2, 3 but all three will be stored in 3 different variables.
I tried doing something this like this:
printf("Enter three numbers: ");
scanf("%d %d %d", one,two,three);
But this does not work!
Here is the entire code:
#include <stdio.h>
int main(void) {
int one,two,three;
printf("Enter three numbers: ");
scanf("%d %d %d", &one,&two,&three);
printf("%d %d %d \n", one,two,three);
}
I tried entering 1, 2, 3 and I got this: 1 134513881 -1218503675
scanf("%d %d %d", &one, &two, &three);
You were close, but scanf doesn't care about the value of those variables (which is great, because they're most likely not initialized), it cares about their addresses so it can dereference them and write inside.
If the number are to be separated by commas as in the example, the commas need to be part of the format string. Check the return of scanf to confirm three fields were input.
#include <stdio.h>
int main(void) {
int one = 0,two = 0,three = 0, ch = 0;
printf("Enter three numbers separated by commas: ");
while ( ( scanf("%d ,%d ,%d", &one, &two, &three)) != 3) {
printf("Please enter three numbers separated by commas: ");
while ( ( ch = getchar ( )) != '\n' && ch != EOF) {
//clear input buffer
}
}
printf("%d %d %d \n", one,two,three);
return 0;
}
As commented by #M.M, the scanf format "%d ,%d ,%d" with a space before the comma will allow the commas to be preceded by any amount of whitespace (including none) to allow input of 1,2,3 or 1 , 2,3. %d also skips any leading whitespace.

C Wrong Answer when squaring

I'm learning C using C by Example. There is a question where we need to take an input integer and square it and print it to the screen. I'm having a strange error, when no matter what Int I enter it give me this output:
Please enter a number.
5
Number = 2686764 Square of Number = 2686760
Here is the program I wrote.
#include <stdio.h>
#include <conio.h> //for getch()
main(){
int number, square;
printf("Please enter a number. \n");
scanf("%d", &number);
square = number*number;
printf("\n Number = %d ", &number);
printf("\t Square of Number = %d", &square);
getch();
}
You are printing the memory address of the number and square variables, not their values.
Try this instead:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);
Your printf statements are incorrect. Do not use & in this case:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);
To add to the above answers ... look at this example:
int number = 5;
printf(" %d\n", number); // Prints value of 'number'.
printf(" %p\n", &number); // Prints address of 'number'.
printf(" %d\n", *(&number)); // Prints value at address of 'number' ...

How to evaluate an expression in a for loop in C programming?

I am trying to print cube of all the numbers from 1 to 20 (n3). I was wondering in my code if
printf("Enter an integer value\n");
is necessary. What's the purpose of that line? Can someone explain? Thanks. It's my first day learning C.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv)
{
int num;
printf("Enter an integer value\n");
scanf("%d", &num);
for ( num=1; num<21; num++){
printf("The cube of %d is %d\n", num, num*num*num);
}
getchar();
}
By using printf() you are printing msg "Enter an integer value" on the screen.
By using scanf() you are taking the value which you have typed on screen into num variable(i.e. initializing num).But in forloop you are reinitializing your num variable to "1" to "21".So printf() and scanf() are just waste of time , here .
if you want a specific numbers cube then just do as follows,
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num;
printf("Enter an integer value\n");
scanf("%d", &num);
printf("The cube of %d is %d\n", num, num*num*num);
getchar();
}
It is a meaningless
printf("Enter an integer value\n");
scanf("%d", &num);
it just display the message and read the value from keyboard, but in for loop when you assign 1 to the variable num then previous value of num override.
it doesn't serve for anything in your code.
You are getting the variable num but you are neglecting it very soon for ( num=1 ...
If you want it as a useful you can do something similar :
int num, i;
printf("Enter an integer value\n");
scanf("%d", &num);
for ( i=1; i<num; i++){ // print the cube of all numbers less than the entered value
printf("The cube of %d is %d\n", i, i*i*i);
}
getchar();
Otherwise, you can just erase those two lines code:
printf("Enter an integer value\n");
scanf("%d", &num);
the line printf("Enter an integer value\n");
display the words "Enter an integer value" to the user of the program,
in your code the next to lines have no meaning, here you ask the user to enter a integer, then save that number:
printf("Enter an integer value\n");
scanf("%d", &num);
because in the next line you set parameter num to be 1.
this next code scan the user input and then calculate the cube:
int main(int argc, char** argv)
{
int num;
printf("Enter an integer value\n");
scanf("%d", &num);
printf("The cube of %d is %d\n", num, num*num*num);
}

Write a program that prints sum, product and quotient of two input integers in C

I'm new to the language and all this overflow problems and integer types are getting on my nerves. here is what I have but when I run it I get,
-bash: syntax error near unexpected token `newline'
The code:
#include <stdio.h>
int main(void)
{
int one, two, s, q, m;
s = one+two
q = one/two
m = one*two
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
printf("The addition of %d and %d is %d", one, two, s);
printf("The integer division of %d divided by %d is %d", one, two, q);
printf("the multiplication of %d and %d is %d", &one, &two, m);
return 0;
}
Thank you
You should perform the calculations after you've got the input.
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
s = one+two;
q = one/two;
m = one*two;
try this:
#include <stdio.h>
int main(void)
{
int one, two;
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
printf("The addition of %d and %d is %d", one, two, (one+two));
printf("The integer division of %d divided by %d is %d", one, two, (one/two));
printf("the multiplication of %d and %d is %d", &one, &two, (one*two));
return 0;
}
You are missing the semicolons after
s = one+two
q = one/two
m = one*two
Plus you should perform the calculations after you read the input but that's a different problem.
These lines makes the problem:
s = one+two
q = one/two
m = one*two
You missed the ; (semicolon)
Change it like:
s = one+two;
q = one/two;
m = one*two;
Also read the input from user first before doing the operations.

An extra input appears

For example when I input 2 for num1 and 3 for num2, I expect to get 8 for the output as soon as I enter the second number. However, the program expects me to input one more integer, and I just input a random number like 242 and it still outputs 8, which means that it does not affect the result. So my question is why is there the third input?
Thank you for your help!
#include "stdafx.h"
int Power (int num1, int num2);
int main ()
{
int a, b;
puts ("Enter two numbers, a and b:\n");
scanf ("%i\n", &a);
scanf ("%i\n", &b);
printf ("%i\n", Power(a, b));
return 0;
}
int Power (int num1, int num2)
{
int sum=1;
for (int i=1; i<=num2; i++){
sum= sum*num1;
}
return sum;
}
Get rid of the newlines: \n, in your scanf format strings, or just use a single scanf, e.g.:
scanf("%i%i", &a, &b);
Or:
scanf ("%i", &a);
scanf ("%i", &b);
Your scanf() doesn't need the "\n".
scanf ("%i", &a);
scanf ("%i", &b);
You should remove the '\n' from your format string in your calls to scanf.

Resources