0
0
Cprogramming~20 mins

Defining structures - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
ACompilation error
B10 5
C5 10
D0 0
Attempts:
2 left
💡 Hint
Remember how structure members are assigned and accessed.
Predict Output
intermediate
2: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;
}
A4
B5
C6
D8
Attempts:
2 left
💡 Hint
Think about how the compiler aligns data in memory.
🔧 Debug
advanced
2: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;
}
ACompilation error: too many initializers
BRuns and prints: Alice 30
CRuntime error: segmentation fault
DCompilation error: missing semicolon
Attempts:
2 left
💡 Hint
Check how many values are given for initialization compared to members.
Predict Output
advanced
2: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;
}
AMeeting on 8/15/2024
BMeeting on 15/8/2024
CCompilation error
DMeeting on 0/0/0
Attempts:
2 left
💡 Hint
Look at how nested structures are initialized and accessed.
🧠 Conceptual
expert
2: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}
};
A3
B2
C0
D50
Attempts:
2 left
💡 Hint
Count the number of initializers in the array.