unexpected output in C program

Pages: 12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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
closed account (E0p9LyTq)
Change the format string in scanf to: "%s %d %d".

String, not char.
To use the %s modifier, name must be big enough to hold the name of the book.
A single char won't work as will only store the trailing null.
closed account (E0p9LyTq)
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.
A char would hold the first character in a supplied string.

And where is it going to put the null terminator?
What happens when the user enters more than one character?

Using a %s modifier for a single char is going to cause undesirable behavior.

The run-time (scanf) thinks it can store a null-terminated string. scanf has no knowledge that only a single character was provided. Therefore scanf will write over memory not belonging to name. Bottom line, scanf is UNSAFE and has been deprecated.
https://social.msdn.microsoft.com/forums/vstudio/en-US/aa71840c-4a09-482a-afa2-e802166d2789/scanf-deprecated

Use C++ I/O instead.
Last edited on
ok i got that it was that "%c" weird error in code blocks i used " %c" instead of "%c" and it worked fine

i am aware that we use %s for string i wanted to input name as single char(I know it sounds weird) btw thanks everyone for help :)

I am getting an error saying

conflicting types for 'display'

but when i changed the float variable to int it ran fine, i think there is some problem with float type can anybody pls explain...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
main( )
{
    struct book
    {
        int a;
        char ch[10] ;
        float s ;
    } ;

struct book b1 = { 2, "silvester", 4.5 } ;

display ( b1.a, b1.ch , b1.s ) ;
}

display ( int n, char *p, float l )
{
    printf ( "\n%d %s %f", n, p, l) ;
}
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.
sorry but i didn't understood what you are trying to explain...
Do you know what function prototypes are?

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.
so i guess the program worked without prototyping when i used int instead of float because default function type is int, right???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

main( )
{
    struct book
    {
        int a;
        char ch[10] ;
        int s ;
    } ;

struct book b1 = { 2, "silvester", 4 } ;


display ( b1.a, b1.ch , b1.s ) ;
}

display ( int n, char *p, int l )
{
    printf ( "\n%d %s %d", n, p, l) ;
}

C++ does not support default int.
i wrote this in c
Topic archived. No new replies allowed.
Pages: 12