No matter what value I enter for x, the output for the value of y is always 1. Any ideas why?
#include <stdio.h>
int main() {
int x, y;
y = scanf("%d", &x);
printf("y = %d\n", y);
return 0;
}
No matter what value I enter for x, the output for the value of y is always 1. Any ideas why?
#include <stdio.h>
int main() {
int x, y;
y = scanf("%d", &x);
printf("y = %d\n", y);
return 0;
}
From scanf(3) - Linux man page:
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
Since scanf return value is the number of items written (in your case, 1, since only 1 int was scanned), not the integer value of the scanned character
int main() {
int x, y, z, n;
n = scanf("%d", &x);
printf("n = %d\n", n); // prints 1
n = scanf("%d%d", &x, &y);
printf("n = %d\n", n); // prints 2
n = scanf("%d%d%d", &x, &y,&z);
printf("n = %d\n", n); // prints 3
return 0;
}