Week 8: Structures, Typedef, and Enumerations — Solution
Week 8: Structures, Typedef, and Enumerations — Solution
Exercise Solutions
Exercise 1: Define and Print a Student Structure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
struct Student {
char name[30];
int score;
};
int main(void) {
struct Student s;
printf("Enter student name and score:\n");
scanf("%s %d", s.name, &s.score);
printf("%s scored %d points.\n", s.name, s.score);
return 0;
}
Example Output:
1
2
3
Enter student name and score:
Alice 95
Alice scored 95 points.
Exercise 2: Add One Second to a User-Input Time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
struct Time {
int hour;
int minute;
int second;
};
struct Time addOneSecond(struct Time t) {
t.second += 1;
if (t.second == 60) { t.second = 0; t.minute += 1; }
if (t.minute == 60) { t.minute = 0; t.hour += 1; }
if (t.hour == 24) { t.hour = 0; }
return t;
}
int main(void) {
struct Time t;
printf("Enter hour (0-23): "); scanf("%d", &t.hour);
printf("Enter minute (0-59): "); scanf("%d", &t.minute);
printf("Enter second (0-59): "); scanf("%d", &t.second);
t = addOneSecond(t);
printf("New time: %02d:%02d:%02d\n", t.hour, t.minute, t.second);
return 0;
}
Example Output:
1
2
3
4
Enter hour (0-23): 23
Enter minute (0-59): 59
Enter second (0-59): 59
New time: 00:00:00
Exercise 3: Map a Month Number to its Name Using Enum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
enum Month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
int main(void) {
int n;
printf("Enter month number (1-12): ");
scanf("%d", &n);
const char *names[] = {
"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
if (n >= JAN && n <= DEC) {
printf("Month %d is %s\n", n, names[n]);
} else {
printf("Invalid month\n");
}
return 0;
}
Example Output:
1
2
Enter month number (1-12): 5
Month 5 is May
Exercise 4: Use Typedef for a Cleaner Struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
typedef struct {
char title[50];
int pages;
} Book;
int main(void) {
Book b;
printf("Enter book title and number of pages:\n");
scanf("%s %d", b.title, &b.pages);
printf("Book: %s, Pages: %d\n", b.title, b.pages);
return 0;
}
Example Output:
1
2
3
Enter book title and number of pages:
CProgramming 500
Book: CProgramming, Pages: 500
Bonus Challenge: Event Log Management
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
typedef struct {
int day, month, year;
} Date;
typedef struct {
int hour, minute, second;
} Time;
typedef struct {
Date date;
Time time;
char message[100];
} Log;
void readLog(Log *l, int i) {
printf("Enter date (day month year) for log %d: ", i);
scanf("%d %d %d", &l->date.day, &l->date.month, &l->date.year);
printf("Enter time (hour minute second) for log %d: ", i);
scanf("%d %d %d", &l->time.hour, &l->time.minute, &l->time.second);
printf("Enter message for log %d: ", i);
getchar();
fgets(l->message, sizeof(l->message), stdin);
}
void printLog(Log l) {
printf("[%04d-%02d-%02d %02d:%02d:%02d] %s",
l.date.year, l.date.month, l.date.day,
l.time.hour, l.time.minute, l.time.second,
l.message);
}
int main(void) {
Log logs[2];
readLog(&logs[0], 1);
readLog(&logs[1], 2);
printf("\nEvent Logs:\n");
printLog(logs[0]);
printLog(logs[1]);
return 0;
}
Example Output:
1
2
3
4
5
6
7
8
9
10
11
Enter date (day month year) for log 1: 26 4 2025
Enter time (hour minute second) for log 1: 14 30 15
Enter message for log 1: System rebooted
Enter date (day month year) for log 2: 26 4 2025
Enter time (hour minute second) for log 2: 16 45 00
Enter message for log 2: User login successful
Event Logs:
[2025-04-26 14:30:15] System rebooted
[2025-04-26 16:45:00] User login successful
This post is licensed under CC BY 4.0 by the author.