Example : A C program to find out the memory occupied by a structure or union. 
#include <stdio.h>
#include <conio.h>

struct teacher
{
   char tname[50];
   int tid;
   float salary;
}str;

union student
{   
   char sname[50];
   int rollno;
   float fee;
}stu;

int main()
{
   printf("\nsize of 1st structure element = %d bytes\n", sizeof(str.tname));
   printf("\nsize of 2nd structure element = %d bytes\n", sizeof(str.tid));
   printf("\nsize of 3rd structure element = %d bytes\n", sizeof(str.salary));
   
   printf("\n\nsize of whole structure = %d bytes\n",sizeof(str));   
   printf("\nsize of 1st union element = %d bytes", sizeof(stu.sname));
   printf("\nsize of 2nd union element = %d bytes", sizeof(stu.rollno));
   printf("\nsize of 3rd union element = %d bytes", sizeof(stu.fee));
    
   printf("\n\nsize of complete union = %d bytes", sizeof(stu));   
   return 0;
}

Output :
size of 1st structure element = 50 bytes
size of 2nd structure element = 4 bytes
size of 3rd structure element = 4 bytes

size of whole structure = 60 bytes[50+04+04+02 extra]

size of 1st union element = 50 bytes
size of 2nd union element = 4 bytes
size of 3rd union element = 4 bytes

size of complete union = 52 bytes [50 due to Max Size+02 extra]

NB : Here a structure/union takes 2 bytes extra memory space for proper padding/packing of structure/union elements.
Example : A C program to store static/constant data in a structure & display them. 
#include <stdio.h>
#include <conio.h>

struct emp
{
	char ename[30];
	int eid;
	float esal;
};
void main()
{	
	struct emp x={"Raman",140,40000};
	
	printf("\n Name of Employee: %s",x.ename);
	printf("\n Employee Id: %d",x.eid);
	printf("\n Employee Salary: %f\n",x.esal);
	
	getch();
}

/*  --------------------  OR  ----------------------*/

#include <stdio.h>
#include <conio.h>
#include <string.h>
struct emp
{
	char ename[30];
	int eid;
	float esal;
}x;

void main()
{
	strcpy(x.ename,"Robert");
	printf("\nEmployee Name is :%s ",x.ename);

	x.eid=52;
	printf("\nEmployee ID is:%d ",x.eid);
	
	x.esal=50000;
	printf("\nEmployee Salary is:%f ",x.esal);	
	
	getch();
}

/*  --------------------  OR  ----------------------*/

#include <stdio.h>
#include <conio.h>
#include <string.h>
struct emp
{
	char ename[30];
	int eid;
	float esal;
}x={"Robert",52,50000};

void main()
{
	printf("\nEmployee Name is :%s ",x.ename);	
	printf("\nEmployee ID is:%d ",x.eid);	
	printf("\nEmployee Salary is:%f ",x.esal);	
	
	getch();
}
Example : A C program to store & copy static/constant data in another structure & display them. 
#include <stdio.h>
#include <conio.h>

struct emp
{
	char ename[30];
	int eid;
	float esal;
};
void main()
{	
	struct emp x={"Raman",140,40000};
	struct emp y;
	y=x;
	
	printf("The output before copying is =");
	printf("\n Name of Employee: %s",x.ename);
	printf("\n Employee Id: %d",x.eid);
	printf("\n Employee Salary: %f\n",x.esal);
	
	printf("\nThe output after copying is =");
	printf("\n Name of Employee: %s",y.ename);
	printf("\n Employee Id: %d",y.eid);
	printf("\n Employee Salary: %f\n",y.esal);
	
	getch();
}

Output :
The output before copying is =
 Name of Employee: Raman
 Employee Id: 140
 Employee Salary: 40000.000000

The output after copying is =
 Name of Employee: Raman
 Employee Id: 140
 Employee Salary: 40000.000000

----------------  OR ----------------

#include <stdio.h>
#include <conio.h>

struct emp
{
	char ename[30];
	int eid;
	float esal;
};
void main()
{	
	struct emp x={"Raman",140,40000};
	struct emp y={"Robert",210,45000};	
	
	printf("The output before copying is =");
	printf("\n Name of Employee: %s",x.ename);
	printf("\n Employee Id: %d",x.eid);
	printf("\n Employee Salary: %f\n",x.esal);
	
	printf("\nThe output after copying is =");
	printf("\n Name of Employee: %s",y.ename);
	printf("\n Employee Id: %d",y.eid);
	printf("\n Employee Salary: %f\n",y.esal);
	
	getch();
} 

Output:

The output before copying is =
 Name of Employee: Raman
 Employee Id: 140
 Employee Salary: 40000.000000

The output after copying is =
 Name of Employee: Robert
 Employee Id: 210
 Employee Salary: 45000.000000
Example : A C program to store dynamic/run time/user given keyboard input data in a structure & display them. 
#include <stdio.h>
#include <conio.h>

struct emp
{
   char ename[30];
   int eid;
   float esal;
};
struct emp x;
void main()
{    	
    printf("Enter Employee Name : ");
    scanf("%s",x.ename);    
    //gets(x.ename);
	
    printf("Enter Numeric Employee Id : ");
    scanf("%d",&x.eid);
	
    printf("Enter Employee Salary : ");
    scanf("%f",&x.esal);

    printf("\nName of Employee: %s",x.ename);
    printf("\nEmployee Id: %d",x.eid);
    printf("\nEmployee Salary: %f\n",x.esal);
    getch();
}
Example : A C program to store multiple data/record in a structure & display them using array of structure. 
#include <stdio.h>
#include <conio.h>

struct emp
{
	char ename[30];
	int eId;
	float esal;
}x[5];

void main()
{
	int i;
	for(i=1;i<=5;i++)
	{
		printf("Enter Employee Name: ");
		scanf("%s",x[i].ename);	
		printf("Enter Numeric Id : ");
		scanf("%d",&x[i].eId);	
		printf("Enter Employee Salary : ");
		scanf("%f",&x[i].esal);	
	}
	
	printf("\nEmployee Name    Employee Id   Employee Salary\n");
	for(i=1;i<=5;i++)
	{
		printf("%s\t\t",x[i].ename);	
		printf("%d\t\t",x[i].eId);		
		printf("%f\t",x[i].esal);
		
		printf("\n");	
	}
	getch();
}
Example : A C++ program to store multiple data/record in a structure & display them using array of structure.
#include <iostream>
using namespace std;

	struct Employee
       {
            int Eid;
            char Ename[30];
            int Eage;
            float Esal;
       };

       int main()
       {
            int i;
            struct Employee Emp[3];        

            for(i=0;i<3;i++)
            {
                cout << "\nEnter details of " << i+1 <<" Employee";

                cout << "\nEnter Employee Id : ";
                cin >> Emp[i].Eid;

                cout << "\nEnter Employee Name : ";
                cin >> Emp[i].Ename;

                cout << "\nEnter Employee Age : ";
                cin >> Emp[i].Eage;

                cout << "\nEnter Employee Salary : ";
                cin >> Emp[i].Esal;
            }

            cout << "\nDetails of Employees are : ";
            for(i=0;i<3;i++)
            {
	       cout << "\n"<< Emp[i].Eid <<"\t"<< Emp[i].Ename <<"\t"<< Emp[i].Eage <<"\t"<<Emp[i].Esal;
            }
  	return 0;
}
Example : A C program to store five subjects marks of a student and display total marks, percentage etc using structure.
#include <stdio.h>

struct student
{
	char sname [30];
	int  submarks[ 5];
	float percentage;
	int  totmarks;	
};
void main()
{
	struct student std;
	int i;
	
	printf("Enter student name: ");
	gets(std.sname);

	printf("Enter 5 subject marks:\n");
	std.totmarks=0;
	for(i=1;i<=5;i++)
	{
	   printf("Enters %d subject marks : ",i);
	   scanf("%d",&std.submarks[i]);
	   std.totmarks=std.totmarks+std.submarks[i];
	}	
	std.percentage=(float)((float)std.totmarks/(float)500)*100;
	printf("\nStudent Name: %s \nTotal Marks: %d \nPercentage Marks Obtained:
           %.2f",std.sname,std.totmarks,std.percentage);	
	getch();
}
Example : A C program to declare pointer variable in a structure to accept data & display them. 
#include<stdio.h>
#include<conio.h>

struct employee
{
	int empid;
	char empname[50];	
	float salary;	
};

void main()
{
	struct employee emp;	// Normal structure variable declaration
	struct employee *pemp;	// Pointer structure variable declaration
	pemp=&emp;
	
	printf("Enter employee name: ");
	gets(pemp->empname);		
	printf("Enter Employee id: ");
	scanf("%d",&pemp->empid);	
	printf("Enter employee salary:");
	scanf("%f",&pemp->salary);

        printf("\nEmployee id is : %d",emp.empid);
	printf("\nEmployee Name is : %s",emp.empname);
	printf("\nEmployee Salary is : %f",emp.salary);	
	
	printf("\nEmployee id is : %d",pemp->empid);
	printf("\nEmployee Name is : %s",pemp->empname);
	printf("\nEmployee Salary is : %f",pemp->salary);
	
	getch();	
}
Example : A C program to explain the concept of Pointer to Structure.
#include <stdio.h>

struct Book
{
    char bname[10];
    char btype[50];
    int price;
    int page;    
};

int main()
{
    //struct Book bk, *ptr;

    struct Book bk;      //Structure variable
    struct Book *ptr;    //Pointer of Structure    
    ptr = &bk;
 
    struct Book bk1[10];  //Array of structure 
    struct Book *ptr1;    //Pointer of Structure
    ptr1 = bk1;  
    
    return 0;
}
Example : A C program to explain the concept of Pointer to Structure Elements.
#include<stdio.h>
#include<conio.h>

struct person
{
	//Pointer structure elements
	char *name;
	int *age;
	float *height;		
};
	struct person *ptr;	
void main()
{	
	char nm[30]="Robert";
	int ag=56;
	float ht=5.10;
	
	//Assigning address of data variables
	ptr->name=nm;
	ptr->age=&ag;
	ptr->height=&ht;
	
	printf("The Person details are: ");
	printf("%s",*ptr->name);
	printf("%d",*ptr->age);
	printf("%f",*ptr->height);	
	getch();	
}
Example : A C program to display the structure elements/values using Pointer (Pointer to Structure).
#include <stdio.h>

struct Book
{
    char bname[10];
    char btype[50];
    int price;
    int page;    
};

int main()
{
    struct Book bk={"Ramayan","Holybook",450,260};      //Structure variable
    
    struct Book *ptr;    //Pointer of Structure
    ptr = &bk;
 
    printf("Book's Name is = : %s\n", ptr->bname);
    printf("Book's Type is = : %s\n", ptr->btype);
    printf("Book's Price is = : %d\n", ptr->price);
    printf("Book's Page are = : %d\n", ptr->page); 
    
    return 0;
}

Output :

Book's Name is = : Ramayan
Book's Type is = : Holybook
Book's Price is = : 450
Book's Page are = : 260

----------------  OR  ------------------

#include <stdio.h>
#include <malloc.h>

struct Book
{
    char bname[10];
    char btype[50];
    int price;
    int page;    
};

int main()
{
    struct Book bk={"Ramayan","Holybook",450,260};      //Structure variable
    
    struct Book *ptr;    //Pointer of Structure
    ptr = &bk;
 
    //Memory allocation for Structure
	ptr = (struct Book*)malloc(sizeof(struct Book));
	
	//check memory availability
	if( ptr == NULL)
	{
		printf("Unable to allocate memory!!!\n");
		exit(0);
	}
	
	printf("Enter Book Name...\n");	
	scanf("%s", ptr->bname); 
	
	printf("Enter Book Type...\n");
	scanf("%s", ptr->btype);
	
	printf("Enter Book Price...\n");
	scanf("%d", &ptr->price);
	
	printf("Enter Book Page...\n");
	scanf("%d", &ptr->page);
	
	
	printf("\nThe Book details are...\n");
	printf("Book Name is : %s\n", ptr->bname);
	printf("Book Type is : %s\n", ptr->btype);
	printf("Book Price is : %d\n", ptr->price);
	printf("Book Page are : %d\n", ptr->page);
	
	//freeing dynamically allocated memory
	free(ptr);	
    
    return 0;
}
Output :

Enter Book Name...
Ramayan
Enter Book Type...
Holybook
Enter Book Price...
460
Enter Book Page...
320

The Book details are...
Book Name is : Ramayan
Book Type is : Holybook
Book Price is : 460
Book Page are : 320

------------------  OR  ------------------

#include <stdio.h>
#include <malloc.h>

struct Book
{
    char bname[10];
    char btype[50];
    int price;
    int page;    
};

int main()
{
    int i;
	struct Book bk={"Ramayan","Holybook",450,260};      //Structure variable
    
    struct Book *ptr;    //Pointer of Structure
    ptr = &bk;
 
    //Memory allocation for Structure
	ptr = (struct Book*)malloc(sizeof(struct Book));
	
	//check memory availability
	if( ptr == NULL)
	{
		printf("Unable to allocate memory!!!\n");
		exit(0);
	}
	
	printf("\nEnter the Book details ...\n");
	for(i=1;i<=2;i++)
	{
		printf("Enter Book Name...\n");	
		scanf("%s", (ptr+i)->bname); 
		
		printf("Enter Book Type...\n");
		scanf("%s", (ptr+i)->btype);
		
		printf("Enter Book Price...\n");
		scanf("%d", &(ptr+i)->price);
		
		printf("Enter Book Page...\n");
		scanf("%d", &(ptr+i)->page);		
	}	
	
	printf("\nThe Book details are...\n");
	for(i=1;i<=2;i++)
	{
		printf("Book Name is : %s\n", (ptr+i)->bname);
		printf("Book Type is : %s\n", (ptr+i)->btype);
		printf("Book Price is : %d\n", (ptr+i)->price);
		printf("Book Page are : %d\n", (ptr+i)->page);
	}	
	
	//freeing dynamically allocated memory
	free(ptr);	
    
    return 0;
}

Output :

Enter the Book details ...
Enter Book Name...
Ramayan
Enter Book Type...
Holybook
Enter Book Price...
460
Enter Book Page...
320

The Book details are...
Book Name is : Ramayan
Book Type is : Holybook
Book Price is : 460
Book Page are : 320
Example : A C program to display the output using Nested of Structures.
#include<stdio.h>
  
struct teacher   
{  
    char tid[20];
    char tname[30];  
    char subtaught[50];  
    int mobno;  
};
  
struct student  
{  
    int rollno;
    char sname[20];
    char section[5];
	  
    struct teacher tr;  
   //Simple Individual Nested Structure
};
  
void main ()  
{  
    struct student stu;
	  
    printf("Enter Student RollNo, Name and Section\n");  
    scanf("%d %s %s",&stu.rollno,stu.sname, stu.section);
    
    printf("\nEnter Teacher Id, Name, Subject Taught and Mobile No\n");  
    scanf("%s %s %s %d",stu.tr.tid,stu.tr.tname, stu.tr.subtaught,&stu.tr.mobno);
	  
    printf("\nStudent Details are\n");  
    printf("Stuent's' Name: %s\nStudent's RollNo: %d\nStudent's Section: %s\nTeacher's Name: %s\nSubject Taught: %s",stu.sname,stu.rollno,stu.section,stu.tr.tname,stu.tr.subtaught);  

    getch();
}
  
Output:
Enter Student RollNo, Name and Section
54
Soha
C

Enter Teacher Id, Name, Subject Taught and Mobile No
B35
B.K.Sahay
English
5451525658

Student Details are
Stuent's' Name: Soha
Student's RollNo: 54
Student's Section: C
Teacher's Name: B.K.Sahay
Subject Taught: Englis

-----------------------  OR  -----------------------
#include<stdio.h>
  
struct student  
{  
    int rollno;
	char fname[20];
	char addr[150];
	
	struct sname
     // Embedded Nested Structure
	{
	   char sfirstname[20];
	   char smidname[20];
	   char slastname[20];		
	}sn;    
}stu;
  
void main ()  
{  
    printf("Enter Student RollNo, Father's Name, Address, First Name, Middle Name and Last Name\n");  
    scanf("%d %s %s %s %s %s",&stu.rollno,stu.fname, stu.addr,stu.sn.sfirstname, stu.sn.smidname, stu.sn.slastname);
    
    printf("\nStudent Details are\n\n");  
    printf("Stuent's First Name: %s\nStuent's Middle Name: %s\nStuent's Last Name: %s\nStudent's RollNo: %d\nStudent's Father Name: %s\nAddress: %s\n",stu.sn.sfirstname,stu.sn.smidname,stu.sn.slastname,stu.rollno,stu.fname,stu.addr);  

    getch();
}
  
Output:
Enter Student RollNo, Father's Name, Address, First Name, Middle Name and Last N
ame
120
T.Prasad
Patna
Raman
Kumar
Vyas

Student Details are
Stuent's First Name: Raman
Stuent's Middle Name: Kumar
Stuent's Last Name: Vyas
Student's RollNo: 120
Student's Father Name: T.Prasad
Address: Patna

Loading

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.