I tried this structure in code blocks, it ran fine for b[0] taking all three value but after that for b1,b2,b3,b4 it takes input for name only can anybody pls explain
#include <stdio.h>
main( )
{
struct book
{
char name ;
int price ;
int pages ;
} ;
struct book b[5] ;
int i ;
for ( i = 0 ; i <= 4 ; i++ )
{
printf ( "\nEnter name, price and pages " ) ;
scanf ( "%c %d %d", &b[i].name, &b[i].price, &b[i].pages ) ;
}
for ( i = 0 ; i <= 4 ; i++ )
printf ( "\n%c %d %d", b[i].name, b[i].price, b[i].pages ) ;
}
output:
Enter name, price and pages a
12
23
Enter name, price and pages b
Enter name, price and pages c
Enter name, price and pages d
Enter name, price and pages e
a 12 23
b garbage garbage
c garbage garbage
d garbage garbage
e garbage garbage
http://www.cplusplus.com/reference/cstdio/scanf/ regarding the format specifier %s:
Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
A char would hold the first character in a supplied string.
That should not be the entire error message. There should be more. Lets try:
$ cat -n conflict.c
1 #include <stdio.h>
2 main( )
3 {
4 struct book
5 {
6 int a;
7 char ch[10] ;
8 float s ;
9 } ;
10
11 struct book b1 = { 2, "silvester", 4.5 } ;
12
13 display ( b1.a, b1.ch , b1.s ) ;
14 }
15
16 display ( int n, char *p, float l )
17 {
18 printf ( "\n%d %s %f", n, p, l) ;
19 }
$ gcc -Wall -Wextra conflict.c -o conflict
conflict.c:3: warning: return type defaults to ‘int’
conflict.c: In function ‘main’:
conflict.c:13: warning: implicit declaration of function ‘display’
conflict.c: At top level:
conflict.c:17: warning: return type defaults to ‘int’
conflict.c:16: error: conflicting types for ‘display’
conflict.c:17: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
conflict.c:13: note: previous implicit declaration of ‘display’ was here
$
That is more like it.
Can you guess what the "implicit declaration of function" is about?
What is a function declaration?
That explains the error on line 16: the implementation differs from the declaration. We don't know how, because the declaration was not written by you.
Line 13: You call display. How does the compiler know what the function signature for display() is, since it has not seen a function prototype for display()?
You have two choices.
1) Add a function prototype for display() BEFORE main.
1 2
// After line 1
void display ( int n, char *p, float l );
Or 2) Move your display function in its entirety before main.
BTW, you're missing the function type for both main and display.