📖 Scenario: You are creating a simple program to store information about a book in a library. Each book has a title, an author, and a year of publication.
🎯 Goal: You will define a structure to hold book information, create a variable of that structure, assign values to it, and then print the book details.
📋 What You'll Learn
Define a structure called Book with three members: title (string), author (string), and year (integer).
Create a variable called myBook of type Book.
Assign the title "The C Programming Language", author "Kernighan and Ritchie", and year 1978 to myBook.
Print the book details in the format: Title: The C Programming Language, Author: Kernighan and Ritchie, Year: 1978.
💡 Why This Matters
🌍 Real World
Structures help organize complex data like records of books, employees, or products in real programs.
💼 Career
Understanding structures is essential for C programming jobs, especially in systems programming, embedded systems, and software development.
Progress0 / 4 steps
1
Define the Book structure
Define a structure called Book with three members: title and author as arrays of 50 characters, and year as an integer.
C
Hint
Use struct Book { ... }; syntax. Use char title[50]; and char author[50]; for strings.
2
Create a Book variable called myBook
Create a variable called myBook of type struct Book.
C
Hint
Use struct Book myBook; to declare the variable.
3
Assign values to myBook
Assign the title "The C Programming Language", author "Kernighan and Ritchie", and year 1978 to the myBook variable. Use strcpy to copy strings.
C
Hint
Use strcpy(destination, source); to copy strings into the structure members.
4
Print the book details
Print the book details using printf in the format: Title: The C Programming Language, Author: Kernighan and Ritchie, Year: 1978.
B. struct Employee e = {id:101, name:"Alice", salary:75000.0};
C. struct Employee e = (101, "Alice", 75000.0);
D. struct Employee e = {101, "Alice", 75000.0};
Solution
Step 1: Check initialization syntax for struct
Structs can be initialized with braces { } listing values in order of members.
Step 2: Evaluate each option
struct Employee e = {101, "Alice", 75000.0}; uses correct brace initialization. struct Employee e; e.id = 101; e.name = "Alice"; e.salary = 75000.0; tries to assign string directly to array (invalid). struct Employee e = (101, "Alice", 75000.0); uses parentheses incorrectly. struct Employee e = {id:101, name:"Alice", salary:75000.0}; uses named initialization which is not standard in C.
Final Answer:
struct Employee e = {101, "Alice", 75000.0}; -> Option D
Quick Check:
Brace list initialization is correct for structs [OK]
Hint: Use braces with values in order to initialize structs [OK]
Common Mistakes:
Assigning strings directly to arrays after declaration
Using parentheses instead of braces
Trying named member initialization (not standard C)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.