- How to model a date with three ints — day, month, year
- How to fill the struct at declaration, or set the fields one at a time
- How to print a date in
dd/mm/yyyyformat using%02d - Why getting the format right (padding with zeros) makes the output look proper
- Why a date struct is the starting point for difference, comparison, and validation programs
Three numbers that travel together
A date is three numbers — day, month, and year. You could keep them in three separate variables, but then you'd have to pass all three around every time you needed to talk about a date. And if you had two dates to compare, that's six variables. A struct fixes that: struct date d; is a single variable holding all three parts of one date.
All three members are int, because a day, a month, and a year are whole numbers. There's no such thing as "the 15.5th of March" — days are integers. Years are integers. Months are integers, usually 1 to 12. So int day, int month, int year is the natural type for each field.
The interesting part is the output formatting. If you print 5/3/2024, it looks a bit rough. If you print 05/03/2024, it looks like a proper date. That's what %02d does — it pads single-digit numbers with a leading zero so the month and day always have two digits. It's a small thing, but it's the difference between "this program works" and "this program is nice to look at".
💡 In one line: struct date { int day; int month; int year; }; is the type. struct date d = {25, 12, 2024}; is one date. Print with %02d/%02d/%d for the dd/mm/yyyy look.
The struct and its fields
Three ints, one for each part:
struct date {
int day;
int month;
int year;
};
Same as every other struct — don't forget the semicolon after the closing brace. The order matters for the initializer list (day first, month, then year), but not for anything else.
| Member | Type | Meaning | Valid range |
|---|---|---|---|
day | int | Day of the month | 1 to 31 (depending on the month) |
month | int | Month of the year | 1 to 12 |
year | int | Year | Usually 1 to 9999, or 0 to whatever |
There's no rule in C that enforces the ranges. You could set d.month = 13 and the struct would happily accept it. It's up to the program to check that the values make sense before printing them. We'll come back to validation in the FAQ, but for this assignment we're just storing and printing.
Complete C Program
Program: define 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's a version that reads the date from the user, which is closer to what you'd write in a real program:
Variant: read the date 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("You entered: %02d/%02d/%d\n", d.day, d.month, d.year);
return 0;
}
The first version hardcodes the date so you can see the struct filled and printed. The second reads it from the user so you can try other dates. Same struct, same fields, same printf — only the fill method is different.
Program Output
Date : 25/12/2024
And for the user-input version, try a date with single-digit day and month:
Enter date (dd mm yyyy): 5 3 2024
You entered: 05/03/2024
Notice that 5 3 2024 prints as 05/03/2024 — the %02d adds the leading zeros. Without it, you'd see 5/3/2024, which is still correct but looks less like a standard date. Try a few other dates — the 1st of January 2000, the 31st of December 1999 — and see how they print.
Step-by-Step Explanation
Short program, quick walk-through.
1. The struct definition
struct date { int day; int month; int year; }; — three ints, one for each part of the date. The names are for humans reading the code; C doesn't care what you call them as long as you use them consistently.
2. Declaring and filling
struct date d = {25, 12, 2024}; creates a date with day 25, month 12, year 2024. The values go into the fields in the order they're declared — day first, month, then year. If you swapped the field order in the struct, you'd have to swap the values here too (or use named fields: {.year = 2024, .month = 12, .day = 25}).
3. Printing the date
printf("%02d/%02d/%d\n", d.day, d.month, d.year); — three format specifiers for three values. The %02d means "print an integer, at least two digits, pad with a leading zero if needed". So 5 becomes 05, 12 stays 12. The %d for year has no width, so it prints as-is. The slashes between them are just literal characters.
4. Why %02d and not just %d?
Because dates are conventionally written with two digits for day and month. 05/03/2024 reads more clearly than 5/3/2024 — it's the format people are used to seeing on forms, tickets, and official documents. The zero-padding is a small detail, but it's exactly the kind of thing that makes a program feel polished.
5. Reading from the user
The scanf variant uses scanf("%d %d %d", &d.day, &d.month, &d.year). The user types three numbers separated by spaces (or any whitespace), and scanf fills the three fields. The & is essential — each field needs its address passed so scanf knows where to write.
6. Where this leads
Once you have a date struct, computing the difference between two dates is the natural next step (that's assignment 15). You'd take two dates, compare them, and work out how many days apart they are. That requires knowing how many days are in each month, handling leap years, and so on. But all of it starts from this struct — two ints, a third int, and a lot of arithmetic.
7. Why not a string?
You could store the date as a string like "25/12/2024", but then any arithmetic would require parsing it back into numbers. Storing three integers keeps the data in a form you can compute with directly. If you ever need to print it, that's just a printf away. This is a general principle: store data in its natural numeric form, format it for display.
🎯 Remember this: a date is three ints — day, month, year. Fill it with {25, 12, 2024} or read from the user with scanf. Print with %02d/%02d/%d for the dd/mm/yyyy format. %02d pads single digits with a leading zero, which makes dates look right.
Frequently Asked Questions
1. Why are the fields ints and not strings?
Because date arithmetic — computing differences, checking leap years, comparing dates — needs numeric values, not text. A day is a number, a month is a number, a year is a number. Storing them as ints lets you add, subtract, and compare them directly. If you stored "25/12/2024" as a string, you'd have to parse it back into numbers every time you wanted to do anything with it, which is slower and more error-prone.
2. What does %02d do exactly?
It prints an integer with at least two digits, padding with a leading zero if needed. So 5 prints as 05, 12 prints as 12, and 100 prints as 100. The 0 in %02d is the padding character (zero instead of space); the 2 is the minimum width. For dates, this is exactly what you want — leading zeros look right for days and months.
3. Should I validate the date (e.g., reject month 13)?
Yes, in a real program. Nothing in C stops you from setting d.month = 13, but a month of 13 doesn't exist. You'd add a check after reading: if (d.month < 1 || d.month > 12) { error }, and similarly for day (which has to be checked against the number of days in that month) and year. For this assignment, assuming valid input is fine; the validation habit is worth adding once you're comfortable with the basics.
4. How would I compare two dates?
The simplest way is year first, then month, then day. if (a.year < b.year) return -1; if (a.year > b.year) return 1; if (a.month < b.month) return -1; and so on. That's the lexicographic order for dates and it works correctly. For "how many days apart", you'd need to convert each date to days since some reference date, subtract, and take the absolute value — that's the next assignment but one.
5. Can I store the date in an array?
Yes, and it's common for storing a list of dates (holidays, deadlines, transaction history). struct date dates[100]; gives you 100 dates, each with its own day, month, and year. You'd fill and print them in a loop, exactly as with any other array of structs. The struct itself doesn't change — only where it lives.
6. What's the difference between this date struct and the C standard library's time functions?
The C standard library has time.h with struct tm and functions like time(), localtime(), and mktime(). Those handle the complexity of real calendars (leap years, time zones, daylight saving). Our struct is a much simpler model, enough for basic date storage and printing. In real code, you'd often use time.h. For learning how structs work, ours is the right size.
💡 Tip: After this, try adding a small check to confirm the month is between 1 and 12, and print an error if it isn't. Then try filling the struct with a "today" function — use time() and localtime() from time.h to get the current date and fill the struct. That connects this simple example to real-world C code.