Why 14:5:3 looks wrong
Imagine a clock that says "14:5:3". Technically it's telling you the right time — 14 hours, 5 minutes, 3 seconds. But it doesn't look like a clock. Real clocks say "14:05:03". The zeros matter. And that's the whole lesson of this assignment in a nutshell: storing the time is easy, but showing it the way people expect takes a small but important formatting touch.
We're going to fill a time struct, then print it in the format a real clock would use. Along the way you'll see three ways to fill the struct — one-line, named-field, and read-from-user — and understand when each one is the right choice.
Filling a time in one line
In the last assignment, we defined the time struct and filled it with a hardcoded value. Here we look at the different ways to fill it. The simplest is the brace form: struct time t = {14, 30, 45}; — one line, three values, done. The values match the fields in order: 14 goes to hours, 30 to minutes, 45 to seconds.
There's also the named form: {.hours = 14, .minutes = 30, .seconds = 45}. It's a few extra characters, but the field names make it clear which value is which, and the order stops mattering. If you ever swap the fields in the struct definition, the named form still fills the right fields — the brace form silently puts 45 into hours and prints "45:30:14" the next time you run it. So for anything beyond two or three fields, the named form is the safer choice.
And when the time comes from the user, there's a third form: scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds). Same struct, same fields, just filled at runtime. All three are worth knowing because each has its place.
💡 In one line: struct time t = {14, 30, 45}; fills in order, {.hours = 14, .minutes = 30, .seconds = 45} fills by name, and scanf fills from the user. Print with %02d:%02d:%02d for HH:MM:SS.
Two ways to set the parts
Here are the two brace forms next to each other. Both create the same time. The first is shorter; the second is safer.
/* Fill in order */
struct time a = {14, 30, 45};
/* Fill by name (C99) */
struct time b = {.hours = 14, .minutes = 30, .seconds = 45};
Both produce the time 14:30:45. The first depends on the order of fields in the struct; the second doesn't. If you're writing code that someone else might edit later, the named form is worth the extra typing.
| Style | Example | Good for | Watch out for |
|---|---|---|---|
| In order | {14, 30, 45} |
Small structs, quick code | Breaks if the fields are reordered |
| By name | {.hours = 14, .minutes = 30, .seconds = 45} |
Real projects, longer structs | Needs C99 or later (fine on modern compilers) |
| From the user | scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds) |
Interactive programs | You need to validate the input (see FAQ) |
Complete C Program
Program: fill a time and print it
#include <stdio.h>
struct time {
int hours;
int minutes;
int seconds;
};
int main(void) {
struct time t = {14, 30, 45};
printf("Time : %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
return 0;
}
And here are the other two forms — first with named fields, then reading from the user:
Variant 1: named fields
#include <stdio.h>
struct time {
int hours;
int minutes;
int seconds;
};
int main(void) {
struct time t = {.hours = 14, .minutes = 30, .seconds = 45};
printf("Time : %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
return 0;
}
Variant 2: read from the user
#include <stdio.h>
struct time {
int hours;
int minutes;
int seconds;
};
int main(void) {
struct time t;
printf("Enter time (hh mm ss): ");
scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds);
printf("Time : %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
return 0;
}
All three versions produce a time that prints as HH:MM:SS. The first is the assignment answer. The second shows the named-field form. The third is what you'd write in a real program — ask the user, then print the time. The & in the scanf is essential; without it, scanf doesn't know where to put the numbers.
Program Output
Time : 14:30:45
And for the user-input version, try single-digit parts to see the padding do its job:
Enter time (hh mm ss): 9 5 3
Time : 09:05:03
The single-digit inputs 9, 5, and 3 print as 09, 05, and 03, thanks to the %02d. Without it, you'd see 9:5:3 — still readable, but not the usual time format. Try a few other times: midnight (0 0 0), noon (12 0 0), and just before midnight (23 59 59).
Step-by-Step Explanation
Short program, quick walk-through.
1. The struct
Same struct time as the last assignment. Three ints — hours, minutes, seconds. Nothing new in the definition.
2. The three fill methods
The brace form {14, 30, 45} fills in order — hours first, then minutes, then seconds. The named form {.hours = 14, .minutes = 30, .seconds = 45} fills by name, so the order doesn't matter. The scanf form fills at runtime: the user types the three numbers, and scanf puts them into the three fields via their addresses. Each method produces the same struct with the same three values.
3. Printing the time
printf("%02d:%02d:%02d\n", t.hours, t.minutes, t.seconds); — three values, three format specifiers. %02d shows two digits with leading zeros, so 9 becomes 09 and 30 stays 30. The colons between them are literal characters. The result reads like a standard time.
4. Why the padding matters
Times without padding look inconsistent: 9:5:3, 14:30:45, 0:0:0, 23:59:59. Four different widths. With padding, they all become the same width: 09:05:03, 14:30:45, 00:00:00, 23:59:59. The columns line up, the format is uniform, and it matches how clocks display times. It's a small formatting detail, but it's the kind of thing that separates a "works" program from a polished one.
5. Reading from the user
The scanf form is what you'd use in a real program. The user types "14 30 45" (or any numbers separated by spaces), and scanf reads them into the three fields. The & is important: &t.hours is the address of the hours field, which is where scanf writes the first number.
6. When to use which
Use the brace form when the time is fixed and known at compile time. Use the named form when you want extra clarity or when the struct has many fields. Use scanf when the time comes from the user. These aren't exclusive — a real program might use scanf for input, then fill a struct with named fields in a function that returns it. Pick the style that fits the situation.
7. Where this leads
Adding two times is the next assignment. The natural approach is to convert each time to seconds-since-midnight, add, then convert back. That's the same "convert to a single number" idea we used for dates. So getting comfortable with the fill patterns now saves you time later.
🎯 Remember this: fill with {14, 30, 45} or {.hours = 14, .minutes = 30, .seconds = 45} or scanf. Print with %02d:%02d:%02d for HH:MM:SS. The padding is what makes times look right.
Frequently Asked Questions
1. Does the order of values matter in the brace form?
Yes. {14, 30, 45} puts 14 into hours, 30 into minutes, and 45 into seconds, because that's the order of the fields in the struct. If you swapped the struct to declare seconds first, the same brace form would put 14 into seconds and 45 into hours — silently wrong. The named form doesn't have this problem.
2. Why %02d and not just %d?
%d prints the integer with no padding: 5 prints as "5", 14 prints as "14". %02d prints with at least two digits, padding with leading zeros: 5 prints as "05", 14 prints as "14". For times, this gives the standard two-digit format that people expect. It's a small detail that makes the output look right.
3. Can I fill the struct after declaring it?
Yes — struct time t; t.hours = 14; t.minutes = 30; t.seconds = 45; works the same. The brace form is only valid at the point of declaration. If the values come from the user, you have to fill the fields one by one (or via scanf), since they're not known at declaration time.
4. What if the user enters 99 for minutes?
The struct will accept it, but 99 minutes doesn't make sense as a time of day. A real program would validate the input: check that hours is 0-23, minutes is 0-59, seconds is 0-59, and reject the input otherwise. Or it would normalise it: 99 minutes = 1 hour 39 minutes. For this assignment, assuming valid input is fine, but the validation habit is worth adding. The next assignment (adding two times) will naturally produce out-of-range values, so you'll need to handle that.
5. What is 14:30:45 in 12-hour format?
2:30:45 PM. The rule is: if hours is 0, it's 12 AM (midnight); if hours is 1-11, it's AM; if hours is 12, it's 12 PM (noon); if hours is 13-23, subtract 12 and it's PM. So 14 becomes 2 PM, 23 becomes 11 PM. To display in 12-hour format, convert at print time: int h = t.hours % 12; if (h == 0) h = 12; printf("%02d:%02d:%02d %s", h, t.minutes, t.seconds, t.hours < 12 ? "AM" : "PM");. It's a useful exercise but not required by this assignment.
6. Can I display the time with milliseconds?
You'd add a fourth field: int milliseconds;. Same struct, same print pattern: printf("%02d:%02d:%02d.%03d\n", t.hours, t.minutes, t.seconds, t.milliseconds);. The %03d pads to three digits, so 5 milliseconds prints as 005. This is common in stopwatch and timing applications, but not in time-of-day. Keep it simple for now.
💡 Tip: After this, try reading a time from the user, then printing both the 24-hour and 12-hour versions of it. For example, "14:30:45" and "2:30:45 PM". Same struct, two different display functions. That's the kind of thing you'll do all the time once you're working with time data in real programs.