There are a number of issues in your code. First and foremost, the %s
specifier (for both the scanf
and printf
functions) expects a string argument (that is, a nul
-terminated array of char
for printf
or an array sufficiently large to hold the input characters plus that terminator, for scanf
); however, you are attempting to read (and print) a single char
value in each of the relevant for
loops.
To fix this, use the %c
format specifier, instead of %s
. However, when you use this, the newline character that is generated when you press the Enter key will be left in the input buffer, and that will be read as the actual char
input on the next iteration of the first for
loop. To clear any such newline (or, indeed other whitespace) from the input before the real input, add a space in the format string before the %c
. Also, when using this, you will need to pass the address of each name
element: scanf(" %c", &name[i]);
.
Further, the printf
function takes the actual values of the variables to be output, rather than their addresses – so remove the &
from the arguments in your printf
calls. (Also, and I assume it's a typo, the price[3]
expression should be price[i]
– the former attempts to access an out-of-bounds element of the price
array.)
Another issue is that, in each of your input loops, you call the scanf
function before you display the relevant prompt. In the code below, I have reversed your printf
and scanf
lines in each of those input loops.
Here's a possible fixed version:
#include<stdio.h>
int main()
{
char name[3];
float price[3];
int i, page[3];
printf("enter the name price and book\n");
for (i = 0; i < 3; i++) {
printf("enter character:\n");
scanf(" %c", &name[i]);
}
for (i = 0; i < 3; i++) {
printf("enter floating point number:\n");
scanf("%f", &price[i]);
}
for (i = 0; i < 3; i++) {
printf("enter digit:\n");
scanf("%d", &page[i]);
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("%c\n", name[i]);
}
for (i = 0; i < 3; i++) {
printf("%f\n", price[i]);
}
for (i = 0; i < 3; i++) {
printf("%d\n", page[i]);
}
return 0;
}