adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C structure contains member variable of another structure type

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
  1.  Structure in C language
  2. Program in C to enter student marks and find the sum, percentage and result (pass /fail)
  3. Passing structure to function program in c
  4. Array of structures program in c

Post a Comment

0 Comments