scanf
's loop is not working for index (which is 2). In the printf
loop, I am getting garbage output for two indexes. I don't know what is happening here.
#include <stdio.h>
int main() {
int i;
char name[3];
float price[3];
int pages[3];
printf("Enter names, prices and pages of 3 books:\n ");
for (i = 0; i <=2; i++) {
scanf("%c%f%d", &name, &price, &pages);
}
printf("what you entered:\n");
for (i = 0; i <=2; i++) {
printf("%c %f %d\n", name[i], price[i], pages[i]);
}
return 0;
}
This program is from the book "Let us C", first page of the structure chapter.
My real output (what I am getting) is:
Enter names, prices and pages of 3 books:
a 100 200 // given by me
b 100 200
// but not able to give third index values
what you entered:
b 100.000000 200
84227675241280636545541341184\.000000 0
0\.000000 70
This is a simple program and according to me, I should get like the below output.
My output is what I am expecting:
Enter names, prices and pages of 3 books:
a 100 200
b 100 200
c 100 200
what you entered:
a 100.000000 200
b 100.000000 200
c 100.000000 200