Challenge: Book Record
Completed
Define a struct Book with a title (one word), pages and price (a double). Read one book and print it like this:
Title: Dune Pages: 412 Price: 9.50
Input
A title (one word), pages and price, separated by spaces.
Output
Three lines as shown.
Example
Input
Dune 412 9.5
Output
Title: Dune Pages: 412 Price: 9.50
Editor
C
Hints
Read a double with %lf.
The title is a string, so no & before b.title.
Solution explanation
The three fields live together in one Book variable and are reached with the dot.
Show solution
#include <stdio.h>
typedef struct {
char title[50];
int pages;
double price;
} Book;
int main() {
Book b;
scanf("%s %d %lf", b.title, &b.pages, &b.price);
printf("Title: %s\n", b.title);
printf("Pages: %d\n", b.pages);
printf("Price: %.2f\n", b.price);
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module