Home  >  Article  >  Backend Development  >  C program uses structure to store inventory system

C program uses structure to store inventory system

PHPz
PHPzforward
2023-09-16 14:17:04965browse

C program uses structure to store inventory system

Structure is a collection of variables of different data types, grouped together by a single name.

Characteristics of structures

The characteristics of structures in C language programming language are as follows-

  • All structural elements of different data types can be combined by using assignment Contents are copied to another structure variable of its type

  • In order to handle complex data types, it is better to create a structure within another structure, which is called a nested structure.

  • You can pass the entire structure, individual elements of the structure, and the address of the structure to the function.
  • Can create structure pointers.

Program

The following is a C programUsing structure to store inventory system -

#include<stdio.h>
#include<conio.h>
void main(){
   struct date{
      int day;
      int month;
      int year;
   };
   struct details{
      char name[20];
      int price;
      int code;
      int qty;
      struct date mfg;
   };
   struct details item[50];
   int n,i;
   printf("Enter number of items:");
   scanf("%d",&n);
   fflush(stdin);
   for(i=0;i<n;i++){
      fflush(stdin);
      printf("Item name:");
      scanf("%s",item[i].name);
      fflush(stdin);
      printf("Item code:");
      scanf("%d",&item[i].code);
      fflush(stdin);
      printf("Quantity:");
      scanf("%d",&item[i].qty);
      fflush(stdin);
      printf("price:");
      scanf("%d",&item[i].price);
      fflush(stdin);
      printf("Manufacturing date(dd-mm-yyyy):");
      scanf("%d-%d-%d",&item[i].mfg.day,&item[i].mfg.month,&item[i].mfg.year);
   }
   printf(" ***** INVENTORY *****</p><p>");
   printf("------------------------------------------------------------------</p><p>");
   printf("S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE</p><p>");
   printf("------------------------------------------------------------------</p><p>");
   for(i=0;i<n;i++)
      printf("%d %-15s %-d %-5d %-5d%d/%d/%d</p><p>",i+1,item[i].name,item[i].code,item[i].qty,item[i].price,item[i].mfg.day,item[i].mfg.month,item[i].mfg.year);
   printf("------------------------------------------------------------------</p><p>");
   getch();
}

Output

When the above program is executed, the following results are produced -

Enter number of items:5
Item name:pen
Item code:12
Quantity:50
price:25
Manufacturing date(dd-mm-yyyy):12-02-2020
Item name:pencil
Item code:15
Quantity:100
price:30
Manufacturing date(dd-mm-yyyy):11-03-2020
Item name:book
Item code:34
Quantity:30
price:60
Manufacturing date(dd-mm-yyyy):15-04-2020
Item name:bag
Item code:39
Quantity:20
price:70
Manufacturing date(dd-mm-yyyy):12-03-2021
Item name:sharpner
Item code:33
Quantity:20
price:40
Manufacturing date(dd-mm-yyyy):12-04-2021
***** INVENTORY *****
------------------------------------------------------------------
S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE
------------------------------------------------------------------
1    pen    12       50          25    12/2/2020
2   pencil  15       100         30    11/3/2020
3    book   34       30          60    15/4/2020
4    bag    39       20          70    12/3/2021
5  sharpner 33       20          40    12/4/2021

The above is the detailed content of C program uses structure to store inventory system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete