-1
#include<stdio.h>

int main()
{
    struct MyBookPrice//tag
    {
        int bookid; //struct member1
        int price;      //struct member2
        char pubdate;   //struct member3

    }bookstruct;

    //assign value to the struct..
    bookstruct.bookid;
    bookstruct.price;
    bookstruct.pubdate;

    printf("\nGive new value to book ID: ");
    scanf("%d",&bookstruct.bookid);

    printf("\nGive new value to book Price: ");
    scanf("%d",&bookstruct.price);

    printf("\nGive new value of book Published Date: ");
    scanf("%s",&bookstruct.pubdate);//should be like 20/12/2017

    //accessing and display struct
    printf("\n New Labell =  %d",bookstruct.bookid);
    printf("\n New Label2 =  RM%d",bookstruct.price);
    printf("\n New Label3 =  %s",bookstruct.pubdate);

    system("pause");
    return 0;
}

hi guys, well from the above code..i can execute the program...and i can enter the scanf value for bookstruct.bookid, bookstruct.price, & book.pubdate successfully...but when it goes down to display the data back...its only showed me the value for bookstruct.bookid and bookstruct.price and..finally crashed..without showing the value of bookstruct.pubdate..

between im using dev-C++5.11..can u guys help me out here...am i wrong somewhere..thx in advance ya..

  • Before you use strings, you need to study arrays. C has no built-in string type. Strings in C are just raw arrays of characters with null termination in the end. – Lundin Nov 21 '17 at 11:58

1 Answers1

1

pubdate is a single char, so cannot read more than 1 character.

Hence the behaviour of your code is undefined.

An alternative would be to use a char[] with a certain number of elements for that member. See Char arrays and scanf function in C

Bathsheba
  • 231,907
  • 34
  • 361
  • 483