In this guide, we will learn how to write a C program to print date of birth using structure. This program will help you understand the usage of structure.
C Program to print a person’s date of birth using structure
The explanation of this program is provided at the end of the code along with the output.
#include <stdio.h>
// Define a structure to hold a date
struct Date {
int day;
int month;
int year;
};
int main() {
// Declare a variable dob of type Date
struct Date dob;
// Input the date of birth and store in structure
printf("Enter date of birth (DD/MM/YYYY): ");
scanf("%d/%d/%d", &dob.day, &dob.month, &dob.year);
// Print the date of birth
printf("Date of birth: %02d/%02d/%d\n", dob.day, dob.month, dob.year);
return 0;
}
Explanation:
- Structure Definition:
- We define a structure named
Date
to hold day, month, and year. This is where we will store the input date, which we can later retrieve it using thedob
variable.
- We define a structure named
- Main Function:
- We declare a variable
dob
of typeDate
to store the date of birth in the structure Date. - The program prompts the user to input the date of birth in the format
DD/MM/YYYY
. - The
scanf
function reads the input and stores the values in structure Date usingdob
variable.
- We declare a variable
- Output:
- The program fetches the data from structure
Date
usingdob
variable and prints the date of birth using theprintf
function, formatting the output asDD/MM/YYYY
.
- The program fetches the data from structure
Output:
Enter date of birth (DD/MM/YYYY): 15/05/1990
Date of birth: 15/05/1990
Leave a Reply