Challenge - 5 Problems
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of structure member assignment
What is the output of this C program?
C
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1;
p1.x = 5;
p1.y = 10;
printf("%d %d\n", p1.x, p1.y);
return 0;
}Attempts:
2 left
💡 Hint
Remember how structure members are assigned and accessed.
✗ Incorrect
The structure 'Point' has two integer members x and y. We assign 5 to x and 10 to y, then print them in order. So output is '5 10'.
❓ Predict Output
intermediate2:00remaining
Size of structure with padding
What is the output of this program printing the size of the structure?
C
#include <stdio.h>
struct Data {
char a;
int b;
};
int main() {
printf("%zu\n", sizeof(struct Data));
return 0;
}Attempts:
2 left
💡 Hint
Think about how the compiler aligns data in memory.
✗ Incorrect
The structure has a char (1 byte) and an int (usually 4 bytes). Due to padding for alignment, the total size is 8 bytes, not 5.
🔧 Debug
advanced2:00remaining
Identify the error in structure initialization
What error will this code produce when compiled?
C
#include <stdio.h>
struct Person {
char name[10];
int age;
};
int main() {
struct Person p = {"Alice", 30, 100};
printf("%s %d\n", p.name, p.age);
return 0;
}Attempts:
2 left
💡 Hint
Check how many values are given for initialization compared to members.
✗ Incorrect
The structure has two members but three values are given in initialization. This causes a compilation error for too many initializers.
❓ Predict Output
advanced2:00remaining
Output of nested structures
What is the output of this program?
C
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Event {
char name[20];
struct Date date;
};
int main() {
struct Event e = {"Meeting", {15, 8, 2024}};
printf("%s on %d/%d/%d\n", e.name, e.date.day, e.date.month, e.date.year);
return 0;
}Attempts:
2 left
💡 Hint
Look at how nested structures are initialized and accessed.
✗ Incorrect
The nested structure Date is initialized with day=15, month=8, year=2024. The print statement outputs these values in order.
🧠 Conceptual
expert2:00remaining
Number of items in an array of structures
Given this code, how many elements does the array 'books' contain?
C
struct Book {
char title[50];
int pages;
};
struct Book books[] = {
{"Book A", 100},
{"Book B", 150},
{"Book C", 200}
};Attempts:
2 left
💡 Hint
Count the number of initializers in the array.
✗ Incorrect
The array 'books' is initialized with three elements, so it contains 3 items.