Structure Assignment 13 — Create a Structure to Represent a Date
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Create a Structure to Represent a Date (Day, Month, Year)

📑 On this page:
  • Three numbers that travel together
  • The struct and its fields
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • 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/yyyy format 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.

A date struct drawn as three boxes stacked vertically: day 25, month 12, year 2024. A calendar icon next to it shows the same date as 25/12/2024.

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
dayintDay of the month1 to 31 (depending on the month)
monthintMonth of the year1 to 12
yearintYearUsually 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.

📖 Related Tutorials

  • 14. Initialize and Display the Date Using a Structure
  • 15. Find the Difference Between Two Dates Using a Structure
  • 12. Calculate the Area and Perimeter of a Rectangle Using a Structure
  • Defining a Structure
  • Accessing Structure Members
  • Structure Initialization
  • Array of Structures
  • All 25 Structure Assignments

Previous Topic: -->> 12. Calculate the Area and Perimeter of a Rectangle Using a Structure   ||   Next Topic: -->> 14. Initialize and Display the Date Using a Structure


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.