Example : A C program to determine the memory occupied by structure & 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

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

NB : Here a strucure/union takes 2 bytes extra memory space for padding/packing the structure/union elements.
Example : A C program to store static 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,"Welcome");
	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();
}
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",pemp->empid);
	printf("\nEmployee Name is : %s",pemp->empname);
	printf("\nEmployee Salary is : %f",pemp->salary);
	
	getch();	
}
Example : A C program to store dynamic data/input data from keyboard at run time  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 : ");
    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();
}

----------  OR  ----------
#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 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 display the structure values using 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

 1,081 total views,  1 views today

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.