Structures – Theory and Programs [Learn to code]

What are Structures? Learn syntax for Structures and different programs on Structures.

  • 095. Define a structure Employee having elements emp_id, name,etc. Accept data and reprint it
    #include<stdio.h>
    #include<conio.h>
     
    struct Employee
    {
     char name[50];
     int emp_id;
     long phone_no;
    };
     
    main()
    {
    	struct Employee e;
    	clrscr();
    	printf("Enter name : ");
    	scanf("%s",&e.name);
    	printf("Enter emp_id: ");
    	scanf("%d",&e.emp_id);
    	printf("Enter Phone Number: ");
    	scanf("%ld",&e.phone_no);
     
    	printf("nnEnter name : %s",e.name);
    	printf("nnEnter Emp Id       : %d",e.emp_id);
    	printf("nnEnter Phone Number : %ld ",e.phone_no);
     
    	getch();
    }
  • 096. Define a structure Student having fields roll_no, name, marks, etc, for 5 students, accept data and reprint
     
    #include<stdio.h>
    #include<conio.h>
     
    struct Student
    {
     char name[50];
     int roll_no;
     int m1,m2,m3;
    };
     
    main()
    {   int i;
    	struct Student s[5];
    	clrscr();
    	for(i=0;i<5;i++)
    	{   printf("nEnter data for Student %d.....n",i+1);
    		printf("Enter name           : ");
    		scanf("%s",&s[i].name);
    		printf("Enter Roll No.       : ");
    		scanf("%d",&s[i].roll_no);
    		printf("Enter marks for sub1 : ");
    		scanf("%d",&s[i].m1);
    		printf("Enter marks for sub2 : ");
    		scanf("%d",&s[i].m2);
    		printf("Enter marks for sub3 : ");
    		scanf("%d",&s[i].m3);
    	}
     
    	for(i=0;i<5;i++)
    	{   printf("nStudent   %dn",i+1);
    		printf("Name    : %sn",s[i].name);
    		printf("Roll No.: %dn",s[i].roll_no);
    		printf("Sub1    : %dn",s[i].m1);
    		printf("Sub2    : %dn",s[i].m2);
    		printf("Sub3    : %dn",s[i].m3);
    	}
     
    	getch();
    }
  • 097. Define a structure Employee having elements emp_id, name, DOB, DOJ etc. Accept data and reprint it. (use structure within structure)
    #include<stdio.h>
    #include<conio.h>
     
    struct Date
    {
    	int mm,dd,yy;
    };
     
    struct Employee
    {
    char name[50];
     int emp_id;
     struct Date DOB,DOJ;
    };
     
    main()
    {   int i;
    	struct Employee e;
    	clrscr();
    		printf("nEnter name           : ");
    		scanf("%s",&e.name);
    		printf("nEnter emp_id.        : ");
    		scanf("%d",&e.emp_id);
    		printf("nEnter Date of Joiningn ");
    		printf("(dd-mm-yy) : ");
    		scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
    		printf("nEnter Date of birthn ");
    		printf("(dd-mm-yy) : ");
    		scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
     
    		printf("nName           : %s",e.name);
    		printf("nEmployee ID    : %d",e.emp_id);
    		printf("nEnter DOJ      : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);
    		printf("nEnter DOB      : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);
     
    	getch();
    }

097. Define a structure Employee having elements emp_id, name, DOB, DOJ etc. Accept data and reprint it. (use structure within structure)

#include<stdio.h>
#include<conio.h>
 
struct Date
{
	int mm,dd,yy;
};
 
struct Employee
{
char name[50];
 int emp_id;
 struct Date DOB,DOJ;
};
 
main()
{   int i;
	struct Employee e;
	clrscr();
		printf("\nEnter name           : ");
		scanf("%s",&e.name);
		printf("\nEnter emp_id.        : ");
		scanf("%d",&e.emp_id);
		printf("\nEnter Date of Joining\n ");
		printf("(dd-mm-yy) : ");
		scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
		printf("\nEnter Date of birth\n ");
		printf("(dd-mm-yy) : ");
		scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
 
		printf("\nName           : %s",e.name);
		printf("\nEmployee ID    : %d",e.emp_id);
		printf("\nEnter DOJ      : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);
		printf("\nEnter DOB      : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);
 
	getch();
}

096. Define a structure Student having fields roll_no, name, marks, etc, for 5 students, accept data and reprint

 
#include<stdio.h>
#include<conio.h>
 
struct Student
{
 char name[50];
 int roll_no;
 int m1,m2,m3;
};
 
main()
{   int i;
	struct Student s[5];
	clrscr();
	for(i=0;i<5;i++)
	{   printf("\nEnter data for Student %d.....\n",i+1);
		printf("Enter name           : ");
		scanf("%s",&s[i].name);
		printf("Enter Roll No.       : ");
		scanf("%d",&s[i].roll_no);
		printf("Enter marks for sub1 : ");
		scanf("%d",&s[i].m1);
		printf("Enter marks for sub2 : ");
		scanf("%d",&s[i].m2);
		printf("Enter marks for sub3 : ");
		scanf("%d",&s[i].m3);
	}
 
	for(i=0;i<5;i++)
	{   printf("\nStudent   %d\n",i+1);
		printf("Name    : %s\n",s[i].name);
		printf("Roll No.: %d\n",s[i].roll_no);
		printf("Sub1    : %d\n",s[i].m1);
		printf("Sub2    : %d\n",s[i].m2);
		printf("Sub3    : %d\n",s[i].m3);
	}
 
	getch();
}

095. Define a structure Employee having elements emp_id, name,etc. Accept data and reprint it

#include<stdio.h>
#include<conio.h>
 
struct Employee
{
 char name[50];
 int emp_id;
 long phone_no;
};
 
main()
{
	struct Employee e;
	clrscr();
	printf("Enter name : ");
	scanf("%s",&e.name);
	printf("Enter emp_id: ");
	scanf("%d",&e.emp_id);
	printf("Enter Phone Number: ");
	scanf("%ld",&e.phone_no);
 
	printf("\n\nEnter name : %s",e.name);
	printf("\n\nEnter Emp Id       : %d",e.emp_id);
	printf("\n\nEnter Phone Number : %ld ",e.phone_no);
 
	getch();
}