I am very new to programming and it seems like I've hit a wall with this code I was experimenting with. I'd appreciate it if someone could explain why this comes out weird and what I should do to fix it.
#include <stdio.h>
int main()
{
int a, _a, b, _b, c, _c, d;
printf("Enter 1 pair of numbers\n");
scanf ( "%d ,%d", &a, &_a );
printf("Enter another pair of numbers\n ");
scanf ( "%d ,%d", &b, &_b );
c = a b;
_c = _a _b;
d = c _c;
printf( "The sum of first numbers in each pair is %d\n", c);
printf( "The sum of second numbers in each pair is %d\n", _c);
printf( "the sum of all the numbers is %d\n", d);
return 0;
}
For some reason, the output is like this:
Enter 1 pair of numbers
3
4
Enter another pair of numbers
5
The sum of first numbers in each pair is 7
The sum of second numbers in each pair is -785649472
the sum of all the numbers is -785649465
Thank you so much!
CodePudding user response:
Your scanf()
function expects to find an input formatted as number , number
, so if you input it like number newline number
, it doesn't assign the second value and you get that "strange" value.
To read the values as you're writing them, you should do something like this:
scanf ( "%d %d", &a, &_a );
CodePudding user response:
scanf ( "%d ,%d", &a, &_a );
tells scanf
to scan for and discard spaces (including other “white space” characters like tab or new-line), then scan for a decimal integer, then scan for and discard spaces, then scan for a comma, then scan for a decimal integer (which may include leading spaces). The input you show does not contain a comma, so the scan for a comma fails when it sees the digit “4” instead. At that point, scanf
stops. It does not continue scanning and does not store any value in &_a
. It returns one to indicate that only one item was assigned. However, your program does not check the return value.
When scanf ( "%d ,%d", &b, &_b );
is executed, the “4” is still in the input stream, unprocessed. So scanf
scans that and assigns it to b
. Thus a
has been set to three and b
has been set to four, and this is why the output for “The sum of the first numbers in each pair” is “7”. Then the space in "%d ,%d"
tells scanf
to scan for and discard spaces, and the comma tells scanf
to scan for a comma. Again, that fails, and scanf
stops and returns one, which your program ignores.
At that point, _a
and _b
have not been assigned values. Per the C standard, they have no fixed values, and the program is permitted to use any representable number each time they are used in an expression.
To scan input that does not contain commas, remove the comma from the format strings. You also do not need spaces, because %d
includes scanning and discarding leading white space. You can merely use scanf("%d%d", &a, &_a);
.
However, also check the return value of scanf
. You can use:
int result = scanf("%d%d", &a, &_a);
if (result != 2)
{
fprintf(stderr, "Error, expected two input numbers, but scanf returned %d.\n", result);
exit(EXIT_FAILURE);
}
Add #include <stdlib.h>
to your program to get a declaration of exit
and a definition of EXIT_FAILURE
.
CodePudding user response:
You don't need to use ,
to separate %d
, because of this when you hit enter it also considers that as input too, you can just do it like this it will tell the scanf()
that all your input will be given formate.
int a, _a, b, _b, c, _c, d;
printf("Enter 1 pair of numbers\n");
scanf ( "%d%d", &a, &_a );
printf("Enter another pair of numbers\n");
scanf ( "%d%d", &b, &_b );
c = a b;
_c = _a _b;
d = c _c;
printf( "The sum of first numbers in each pair is %d\n", c);
printf( "The sum of second numbers in each pair is %d\n", _c);
printf( "the sum of all the numbers is %d\n", d);
return 0;
CodePudding user response:
You are using a comma in the format strings
scanf ( "%d ,%d", &a, &_a );
scanf ( "%d ,%d", &b, &_b );
If you are not going to enter it then remove it from the format strings as for example
scanf ( "%d %d", &a, &_a );
scanf ( "%d %d", &b, &_b );
CodePudding user response:
A scanf
format specifier can be composed from three separate kinds of elements:
%
-specifiers. Things like%s
and%f
and%s
causescanf
to (attempt to) parse a value and assign it to one of your variables.- Whitespace. Whitespace in a format string causes
scanf
to skip and ignore whitespace. But the whitespace in the format string doesn't have to exactly match the whitespace that is entered — as far asscanf
is concerned, all whitespace is whitespace, and that includes newlines. - Literal characters, which are basically anything that's not whitespace or a
%
-specifier. Literal characters in the format string mean thatscanf
must see those same characters in the input, exactly.
Also, most (though not quite all) format specifiers automatically skip leading whitespace all by themselves.
Your problem was that you had a literal character (a comma) in your format string, and this caused scanf
to look for that character in the input, and fail when it wasn't found.
Since it's always possible for scanf
to fail and not parse the values you expected — and therefore not fill in your variables with anything — it's highly recommended to always check `scanf's return value. For your program that might look like this:
printf("Enter 1 pair of numbers\n");
if(scanf("%d ,%d", &a, &_a) != 2) {
printf("input error!\n");
exit(1);
}
printf("Enter another pair of numbers\n");
if(scanf("%d ,%d", &b, &_b != 2) {
printf("input error!\n");
exit(1);
}