One Book, Three Facts
MediumThe struct Book definition at the top of the template is written for you, and so is the input handling: one integer is read with scanf, and a run that supplies no number prints no page count and stops. Everything else is this lesson's material in four places. print_book takes a struct Book by value and prints it on one line as Deep C, 300 pages, $19.99, which is %s, %d and $%.2f fed from book.title, book.pages and book.price. The parameter is a copy, so anything you assign to it inside the function is invisible to the caller, which is chapter 3's rule with a bigger object in it. same_book returns 1 when two books match and 0 when they do not, and it has to be written member by member because a == b does not compile: == is not defined for structs. Give each member the operator its type deserves, so strcmp(a.title, b.title) == 0 for the char array, since == on two arrays compares addresses rather than characters, and plain == for the page count and the price. Do not reach for memcmp, which compares the padding bytes as well and those belong to no member. Then the three books, all built with designated initializers. first is Deep C at 300 pages and 19.99, second is the same title and price with the page count you just read, and placeholder names only its title, Untitled, which is the point of it: a member left out of a designated initializer is zeroed, so it prints 0 pages and $0.00 without you writing either. The last line prints both sizes, sizeof(struct Book) and the three member sizes added together, and the two numbers do not agree because the compiler inserts padding to keep the double aligned. Compute both rather than typing them in, since sizeof is the only answer that stays right. main returns 0 on every path, including the one that gives up on the input, because the checker treats a nonzero exit status as a failure however correct the printed output looks.
Success Criteria
Your code must pass 4 test case(s) to complete this exercise. 3 hint(s) are available if you need help.
Sign in to track your progress
You can work on exercises as a guest, but sign in to track your progress and save your submissions.