The Program in C that uses a function that takes two date (day, month, year) structure objects as arguments and returns the structure with later date is given below:
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Date getLaterDate(struct Date d1, struct Date d2) {
if (d1.year > d2.year) {
return d1;
} else if (d1.year == d2.year) {
if (d1.month > d2.month) {
return d1;
} else if (d1.month == d2.month) {
if (d1.day > d2.day) {
return d1;
} else {
return d2;
}
} else {
return d2;
}
} else {
return d2;
}
}
int main() {
struct Date date1, date2, later_date;
printf("Enter the first date (dd mm yyyy): ");
scanf("%d %d %d", &date1.day, &date1.month, &date1.year);
printf("Enter the second date (dd mm yyyy): ");
scanf("%d %d %d", &date2.day, &date2.month, &date2.year);
later_date = getLaterDate(date1, date2);
printf("Later date is: %d/%d/%d\n", later_date.day, later_date.month, later_date.year);
return 0;
}
Output:
Enter the first date (dd mm yyyy): 08
08
1999
Enter the second date (dd mm yyyy): 09
05
1997
Later date is: 8/8/1999
Pro-Tips💡
This program defines a struct “Date” with three members: day, month, and year. It then defines a function “getLaterDate()” that takes two structs of type “Date” as input and compares them by their year, month, and day members.
It returns the struct that has the later date.
In the main function, it prompts the user to input two dates, it stores them in two structs of type “Date” and then pass them as input to the “getLaterDate()” function.
The function compares the two dates and returns the struct that has the later date. The program then prints the later date in the format of day/month/year.
Please note that the program uses scanf(“%d %d %d”,…) to read the input date, and it does not check for the validity of the input, you may want to add some validation on the input to make sure that the input is a valid date.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.