0
0
Cprogramming~20 mins

Accessing structure members - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing structure members
📖 Scenario: You are working on a simple program to store and display information about a book. You will create a structure to hold the book's title and number of pages, then access and print these details.
🎯 Goal: Build a C program that defines a struct for a book with a title and page count, assigns values to its members, and prints them.
📋 What You'll Learn
Define a struct named Book with members title (string) and pages (integer).
Create a variable of type Book named myBook.
Assign the title "C Programming" to myBook.title.
Assign the number 250 to myBook.pages.
Print the book's title and page count using printf.
💡 Why This Matters
🌍 Real World
Structures help organize related data together, like storing information about books, students, or products in a program.
💼 Career
Understanding structures is essential for C programming jobs, especially in systems programming, embedded systems, and software development where grouping data logically is important.
Progress0 / 4 steps
1
Define the Book structure
Define a struct named Book with two members: title as a character array of size 50, and pages as an integer.
C
Need a hint?

Use struct Book { char title[50]; int pages; }; to define the structure.

2
Create a Book variable and assign values
Create a variable named myBook of type struct Book. Assign the string "C Programming" to myBook.title using strcpy, and assign the integer 250 to myBook.pages. Include #include <string.h> at the top.
C
Need a hint?

Remember to include #include <string.h> to use strcpy.

3
Access and print the structure members
Inside the main function, use printf to print the book's title and page count from myBook. Use myBook.title and myBook.pages to access the members.
C
Need a hint?

Use printf("Title: %s\n", myBook.title); and printf("Pages: %d\n", myBook.pages); to print.

4
Run and check the output
Run the program and ensure it prints exactly:
Title: C Programming
Pages: 250
C
Need a hint?

Make sure your output matches exactly, including capitalization and spacing.