- How to fill a date struct at the same line where you declare it
- How to use named fields (
.day = 25) so the order stops mattering - How to read a date from the user with
scanf - How to print the date in the standard
dd/mm/yyyyformat - Why the
%02dpadding is what makes the date look right
Filling a date in one line
In the last assignment, we defined the date struct and printed one filled with hardcoded values. Here we look at the different ways to fill it. The simplest is the brace form: struct date d = {25, 12, 2024}; — one line, three values, done. The values match the fields in order: 25 goes to day, 12 to month, 2024 to year.
There's also the named form: {.day = 25, .month = 12, .year = 2024}. 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 2024 into day and prints "2024/25/12" the next time you run it. So for anything beyond two or three fields, the named form is the safer choice.
Both forms fill the same date. And when the date comes from the user, there's a third form: scanf("%d %d %d", &d.day, &d.month, &d.year). Same struct, same fields, just filled at runtime instead of at compile time. All three are worth knowing because each has its place.
💡 In one line: struct date d = {25, 12, 2024}; fills in order, {.day = 25, .month = 12, .year = 2024} fills by name, and scanf fills from the user. Print with %02d/%02d/%d for dd/mm/yyyy.
Two ways to set the parts
Here are the two brace forms next to each other. Both create the same date. The first is shorter; the second is safer.
/* Fill in order */
struct date a = {25, 12, 2024};
/* Fill by name (C99) */
struct date b = {.day = 25, .month = 12, .year = 2024};
Both produce a date of 25 December 2024. 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 | {25, 12, 2024} |
Small structs, quick code | Breaks if the fields are reordered |
| By name | {.day = 25, .month = 12, .year = 2024} |
Real projects, longer structs | Needs C99 or later (fine on modern compilers) |
| From the user | scanf("%d %d %d", &d.day, &d.month, &d.year) |
Interactive programs | You need to validate the input (see FAQ) |
Complete C Program
Program: fill a date and print it
#include <stdio.h>
struct date {
int day;
int month;
int year;
};
int main(void) {
struct date d = {25, 12, 2024};
printf("Date : %02d/%02d/%d\n", d.day, d.month, d.year);
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 date {
int day;
int month;
int year;
};
int main(void) {
struct date d = {.day = 25, .month = 12, .year = 2024};
printf("Date : %02d/%02d/%d\n", d.day, d.month, d.year);
return 0;
}
Variant 2: read from the user
#include <stdio.h>
struct date {
int day;
int month;
int year;
};
int main(void) {
struct date d;
printf("Enter date (dd mm yyyy): ");
scanf("%d %d %d", &d.day, &d.month, &d.year);
printf("Date : %02d/%02d/%d\n", d.day, d.month, d.year);
return 0;
}
All three versions produce a date that prints as dd/mm/yyyy. 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 date. The & in the scanf is essential; without it, scanf doesn't know where to put the numbers.
Program Output
Date : 25/12/2024
And for the user-input version, try a single-digit day and month:
Enter date (dd mm yyyy): 5 3 2024
Date : 05/03/2024
The single-digit inputs 5 and 3 print as 05 and 03, thanks to the %02d. Without it, you'd see 5/3/2024 — still readable, but not the usual date format. Try a few other dates: the 1st of January 2000, the 31st of December 1999, or a leap-year date like 29/02/2024.
Step-by-Step Explanation
Short program, quick walk-through.
1. The struct
Same struct date as the last assignment. Three ints — day, month, year. Nothing new in the definition.
2. The three fill methods
The brace form {25, 12, 2024} fills in order — day first, then month, then year. The named form {.day = 25, .month = 12, .year = 2024} 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 date
printf("%02d/%02d/%d\n", d.day, d.month, d.year); — three values, three format specifiers. %02d for day and month shows two digits with leading zeros; %d for year shows whatever width is needed. The slashes between them are literal characters. The result reads like a standard date.
4. Why the padding matters
Dates with single-digit day or month look inconsistent without padding: 5/3/2024, 12/3/2024, 5/11/2024. Three different widths. With padding, they all become two-digit: 05/03/2024, 12/03/2024, 05/11/2024. The columns line up, the format is uniform, and it matches how dates appear on official documents. 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 "25 12 2024" (or "25/12/2024" with slashes — scanf is pretty lenient about separators), and scanf reads the three numbers into the three fields. The & is important: &d.day is the address of the day field, which is where scanf writes the first number.
6. When to use which
Use the brace form when the date 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 date 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
The next assignment computes the difference between two dates. That requires reading two dates (either hardcoded or from the user), then doing the arithmetic to figure out how many days apart they are. All the fill-and-print machinery you've seen here is still in play — the only new thing is the calculation. So getting comfortable with the fill patterns now saves you time later.
🎯 Remember this: fill with {25, 12, 2024} or {.day = 25, .month = 12, .year = 2024} or scanf. Print with %02d/%02d/%d for dd/mm/yyyy. The padding is what makes dates look standard.
Frequently Asked Questions
1. Does the order of values matter in the brace form?
Yes. {25, 12, 2024} puts 25 into day, 12 into month, and 2024 into year, because that's the order of the fields in the struct. If you swapped the struct to declare year first, the same brace form would put 25 into year and 2024 into day — 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", 12 prints as "12". %02d prints with at least two digits, padding with leading zeros: 5 prints as "05", 12 prints as "12". For dates, this gives the standard two-digit-day, two-digit-month 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 date d; d.day = 25; d.month = 12; d.year = 2024; 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 31 for February?
The struct will accept it — nothing in C prevents month 2 from having day 31. But 31 February doesn't exist. A real program would validate the input: check that the month is 1-12, then check that the day is between 1 and the number of days in that month (28, 29, 30, or 31 depending on the month and leap year). For this assignment, assuming valid input is fine, but the validation habit is worth picking up. It's the next natural step after you're comfortable with filling and printing.
5. Does scanf accept "25/12/2024" with slashes?
Yes — scanf("%d %d %d", ...) skips whitespace between values, and since "/" is not whitespace, the format would need to be "%d/%d/%d" to match slashes literally. If the user types "25/12/2024" with scanf("%d %d %d", ...), scanf reads 25, then hits "/", which doesn't match " %d" (scanf expects whitespace or a number here), and stops. So it's best to either tell the user to use spaces, or use "%d/%d/%d" with a note that the input must use slashes. For maximum flexibility, read the whole line and parse it — but that's a bigger topic.
6. Can I display the date with month names?
Yes — use a lookup table. const char *names[] = {"Jan", "Feb", ..., "Dec"}; and then print names[d.month - 1] (subtracting 1 because arrays are 0-indexed and months are 1-indexed). So printf("%02d %s %d\n", d.day, names[d.month - 1], d.year) prints "25 Dec 2024". Same struct, just a different print format.
💡 Tip: After this, try adding a small function that checks whether a date is valid: month 1-12, day between 1 and the days-in-month for that month, with February 29 handled by checking leap year. It's a good exercise in if-else ladders and it prepares you for the difference-between-dates program, which needs to know how many days each month has.