C language program Examples using Structure


Content 



1. Global Structure Example

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

/* Global Structure */
struct Distance
{
int feet;
float inches;
};

struct Distance d1; /* Global variable */

int main ()
{
struct Distance d2; /* Local variable */
clrscr();

d1.feet = 23;
d1.inches = 7.5;

d2.feet = 14;
d2.inches = 2.5;

printf( "\n %d\'-%f\"", d1.feet, d1.inches );
printf( "\n %d\'-%f\"", d2.feet, d2.inches );

getch();
return 0;
}

Output:

23'-7.500000"
14'-2.500000"





2. Local Structure Example

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

int main ()
{
// Local Structure
struct Distance
{
int feet;
float inches;
}d1;

struct Distance d2; // Local variable
clrscr();

d1.feet = 23;
d1.inches = 7.5;

d2.feet = 14;
d2.inches = 2.5;

printf( "\n %d\'-%f\"", d1.feet, d1.inches );
printf( "\n %d\'-%f\"", d2.feet, d2.inches );

getch();
return 0;
}

Output:

12'-9.500000"
14'-2.500000"






3. Show the data of item by the code

#include<stdio.h>
#include<conio.h>
struct item
{
int code;
char name[20];
int qty;
};
void main()
{
item it[10];
int n,i,icode,flag=0;
clrscr();
printf("How many item?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Item#%d\n",i);
printf("Code,Name,Quantity:");
scanf("%d%s%d",&it[i].code,it[i].name,&it[i].qty);
}
       printf("Enter item code for the item you want to view:");
       scanf("%d",&icode);
       for(i=0;i<n;i++)
{
if(it[i].code==icode)
{
flag=1;
printf("name:%s\t Quantity:%d\n",it [i].name,it[i].qty);
break;
}
}
if(flag==0)
{
printf("Item not found...");
}      
getch();
}

Output:

How many Items?2
Item#0
Code,Name,Quantity:101 Salt 45
Item#1
Code,Name,Quantity:201 Tea 20
Enter item code for the item you want to view:201
Name:Tea Quantity:20





4. Anonymous Structure Example

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

//  struct Dist
struct
{
int feet;
float inches;
}d1, d2 = {23, 7.5};

int main()
{
clrscr();

d1.feet = 14;
d1.inches = 8.5;

printf( "\n d1 : %d\'-%.1f\"", d1.feet, d1.inches);
printf( "\n d2 : %d\'-%.1f\"", d2.feet, d2.inches);

getch();
   return 0;
}

Output:

d1 : 14'-8.5"
d2 : 23'-7.5"





5. Sort the item data by code in structure

#include<stdio.h>
#include<conio.h>
struct item
{
int code;
char name[20];
int qty;
};
void main()
{
item it[10],t;
int n,i,j;
clrscr();
printf("How many items?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Item#%d\n",i);
printf("Code,Name,Quantity:");
scanf("%d%s%d",&it[i].code,it[i].name,&it [i].qty);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(it[i].code>it[j].code)
{
t=it[i];it[i]=it[j];it[j]=t;
}
}
    }
    printf("After sorting on code\n");
    for(i=0;i<n;i++)
    {
     printf("%d\t%s\t%d\n",it[i].code,it[i].name,it [i].qty);
     }
     getch();
}

Output:

How many items?4
Item#0
Code,Name,Quantity:201  Soap  187
Item#1
Code,Name,Quantity:401  Toothpast  35
Item#2
Code,Name,Quantity:101  Salt  20
Item#3
Code,Name,Quantity:301  Coffee  50
After sorting on code
101  Salt  20
201  Soap  187
301  Coffee  50
401  Toothpast  35






6. Array of Structure

#include<stdio.h>
struct book
{
int id;
char name[40];
};

void main()
{
struct book b[10];
int i,n;
printf("Enter total number of book : ");
scanf("%d",&n);
for(i=0;i<n;i++)
            {
            printf("\nEnter book name and id of Book %d:\n",i+1);
        scanf("%s%d",&b[i].name,&b[i].id);
            }
    for(i=0;i<n;i++)
            {
             printf("\nBook %d:\n",i+1);
         printf("Book id = %d  ",b[i].id);
         printf("Book name= %s",b[i].name);
             }

}

Output:

Enter total number of book : 3

Enter book name and id of Book 1
BeTheDeveloper.com 23
Enter book name and id of Book 2
skyNils.com 14
Enter book name and id of Book 3
MyQuotes 10

Book 1:
Book id = 23 Book name= EasyProgramming24.com

Book 2:
Book id = 14 Book name= w3school.com

Book 3:
Book id = 10 Book name= Tupfr







7. Product information using Structure

#include<stdio.h>
#include<conio.h>
struct product
{
int id;
char name[20];
char piece;
int price;
int total[5];
}p[5];
void main()
{
int total[5],i,n;
int n1;
clrscr();
printf("\t\tEnter the product information");
printf("\n\n\nEnter the products Record:>>");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
total[i]=0;
printf("\n\nEnter the product ID:>>");
                   scanf("%d",&p[i].id);
       printf("Enter the products Name:>>");
       scanf("%s",&p[i].name);
      printf("\n\nEnter the product pieces:>>");
       scanf("%d",&p[i].piece);
       printf("\n\nEnter the product price:>>");
       scanf("%d",&p[i].price);
       total[i]=p[i].piece*p[i].price;
       printf("\n\n\n\t\t\t Total::>>%d",total[i]);
}
printf("\n\n\t\t enter pid:");
scanf("%d",&n1);
for(i=0;i<=n;i++)
{
if(p[i].id==n1)
{
printf("ID:==>%d Total:>>%d\n",p[i].id,p [i].name);
}
}
getch();
}

Output:

Enter the product information


Enter the products Record:>>1

Enter the products Name:>>Coffee
            
       Enter the product pieces:>>10
     
        Enter the product price:>>35

Total::>>350








8. Example to Student tabilar Formate

#include<stdio.h>
#include<conio.h>
struct studentResult
{
int no;
char name[20];
int sub[3];
int total;
}stu[3];
void main()
{
int i,j,k,l;
clrscr();
for(i=0;i<3;i++)
{
clrscr();
printf("\n\n\t\t Enter The Data For Student [%d]:>>",i);

printf("\n\t Enter No:>>");
scanf("%d",&stu[i].no);

printf("\n\t Enter Name:>>");
scanf("%s",&stu[i].name);

printf("\n\t\t\t Enter Subject Marks");
for(j=0;j<3;j++)
{
printf("\n\t Subject[%d]:>>",j);
scanf("%d",&stu[i].sub[j]);
}
for(k=0;k<3;k++)
{
stu[k].total=0;
for(l=0;l<3;l++)
{
stu[k].total=stu[k].total+stu [k].sub[i];
}
}
}
clrscr();
printf("\n\n\n\t\t\tSTUDENT TABILAR FORMATE");
printf("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n No \t\t Name\t\t\t Total");
for(i=0;i<3;i++)
{
printf("\n\n\n%d\t\t%s\t\t\t%d",stu[i].no,stu [i].name,stu[i].total);
}
getch();
}

Output:

Enter The Data For Student
1 Abhishek 180
2 Kishan 150
3 Kaushal 130







9. Nesting of Structure

#include<stdio.h>
struct detail
{
   char name[10];
   float price;
};
struct item
{
    int id;
struct detail d;
};
void main()
{
struct item itm;
printf("Enter item name, id and price ");
scanf("%s%d%f",itm.d.name,&itm.id,&itm.d.price);
printf("Entered details are:\n");
printf("Id: %d\nName: %s  Price: %f", itm.id , itm.d.name, itm.d.price);
}

Output:

Enter item name, id and price
tea 23 150
Entered details are:
Id: 23
Name: tea  Price: 150






10. C program to illustrate the concept of unions


#include <stdio.h>
void main()
{
     union number
     {
         int  n1;
         float n2;
     };
     union number x;
 
     printf("Enter the value of n1: ");
     scanf("%d", &x.n1);

     printf("Value of n1 = %d", x.n1);
     printf("Enter the value of n2: ");
     scanf("%f", &x.n2);
     printf("Value of n2 = %f", x.n2);
}

Output:

Enter the value of n1: 10
Value of n1 = 10
Enter the value of n2: 50
Value of n2 = 50.000000





11. union example

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

union Abc
{
char c;
int i;
float f;
};

int main ()
{
union Abc x;
clrscr();

x.c = 'N';
printf( "\n x.c = %c", x.c );

x.i = 10;
printf( "\n x.i = %d", x.i );

x.f = 1993.5;
printf( "\n x.f = %f", x.f );

getch();
}

Output:

x.c = N
x.i = 10
x.f = 1993.500000





12. differentiate Structure and Union

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

union ABC
{
char a;
int b;
float c;
};

struct XYZ
{
char x;
int y;
float z;
};

int main()
{
union ABC p;
struct XYZ q;
clrscr();

printf( "\n Size of Structure : %d", sizeof(struct XYZ) );
q.x = 'N';
q.y = 10;
q.z = 14.345;
printf( "\n q.x = %c\n q.y = %d\n q.z = %f\n", q.x, q.y, q.z );

printf( "\n Size of Union : %d", sizeof(union ABC) );
p.a = 'N';
printf( "\n p.a : %c", p.a );
p.b = 10;
printf( "\n p.b : %d", p.b );
p.c = 14.345;
printf( "\n p.c : %f", p.c );

getch();
return 0;
}

Output:

Size of Structure : 9
q.x = N
q.y = 10
q.z = 14.345000

Size of Union : 4
p.x = N
p.y = 10
p.z = 14.345000





13. C program to find the size of a union 

#include <stdio.h>
void main()
{
     union sample
     {
         int   m;
         float n;
         char  ch;
     };
     union sample u;
 
     printf("The size of union = %d", sizeof(u));

     u.m = 25;
     printf("%d %f %c", u.m, u.n, u.ch);

     u.n = 0.2;
     printf("%d %f %c", u.m, u.n, u.ch);

     u.ch = 'p';
     printf("%d %f %c", u.m, u.n, u.ch);

}

Output:

The size of union = 4
25 0.000000
1045220557 0.200000
1045220464 0.199999




14. enumeration type ( enum )

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

enum days_of_week { sun, mon, tue, wed, thur, fri, sat };

int main ()
{
enum days_of_week day1, day2;
   int diff;
clrscr();

day1 = mon;
day2 = thur;

diff = day2 - day1;

printf( " Days between day1 & day2 = %d \n", diff );

if( day1 < day2 )
printf( " Day1 comes before Day2." );

getch();
return 0;
}

Output:

Days between day1 & day2 = 3
Day1 comes before Day2.





15. enum example

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

enum Colors { Red, Green, Blue };

int main()
{
enum Colors c1, c2;
int d;
clrscr();

c1 = Red;
c2 = Green;

d = c2 - c1;
printf( "\n Difference = %d", d );

if( c1 < c2 )
printf( "\n RED comes before GREEN." );
else
printf( "\n GREEN comes before RED." );

getch();
return 0;
}

Output:

Difference = 1
RED comes before GREEN.


Reactions

Post a Comment

0 Comments