Program in C structure contains member variable (structure element) of another structure type
#include<stdio.h> #include<conio.h> struct Date { int day; int month; int year; }; struct Student { int roll; char name[30]; char clas[10]; struct Date dob; }; int main() { struct Student s1; printf("Enter following studetnt details.\n"); printf("Roll No. :-"); scanf("%d",&s1.roll); printf("Name :-"); fflush(stdin); gets(s1.name); printf("Class :-"); scanf("%s",s1.clas); printf("Date of Birth :-"); scanf("%d%d%d",&s1.dob.day,&s1.dob.month,&s1.dob.year); printf("\nYour given details.\n"); printf("Roll No. :- %d",s1.roll); printf("\nName :- %s",s1.name); printf("\nClass :- %s",s1.clas); printf("\nDate of birth :- %d/%d/%d",s1.dob.day,s1.dob.month,s1.dob.year); return 0; }
Output:-
Using Function
#include<stdio.h> #include<conio.h> struct Date { int day; int month; int year; }; struct Student { int roll; char name[30]; char clas[10]; struct Date dob; }; void Input(struct Student *s1) { printf("Enter following studetnt details.\n"); printf("Roll No. :-"); scanf("%d",&s1->roll); printf("Name :-"); fflush(stdin); gets(s1->name); printf("Class :-"); scanf("%s",s1->clas); printf("Date of Birth :-"); scanf("%d%d%d",&s1->dob.day,&s1->dob.month,&s1->dob.year); } void Show(struct Student *s1) { printf("\nYour given details.\n"); printf("Roll No. :- %d",s1->roll); printf("\nName :- %s",s1->name); printf("\nClass :- %s",s1->clas); printf("\nDate of birth :- %d/%d/%d",s1->dob.day,s1->dob.month,s1->dob.year); } int main() { struct Student s2; Input(&s2); Show(&s2); return 0; }
Output:-
Related Programs
0 Comments