Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to store inventory system using structures
In C programming, an inventory management system can be efficiently implemented using structures to store and organize product information. Structures allow us to group related data of different types under a single entity, making it perfect for managing inventory details like item names, codes, quantities, prices, and manufacturing dates.
Syntax
struct structure_name {
datatype member1;
datatype member2;
/* more members */
};
Features of Structures
The key features of structures in C programming are −
Enables grouping of different data types under a single name
Supports copying entire structure contents using assignment operator
Allows creation of nested structures for complex data handling
Enables passing structures to functions as parameters
Supports creation of structure pointers for dynamic memory management
Example: Inventory Management System
The following program demonstrates how to create and manage an inventory system using nested structures −
#include <stdio.h>
#include <string.h>
int 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);
for(i = 0; i < n; i++) {
printf("\nItem %d:<br>", i + 1);
printf("Item name: ");
scanf("%s", item[i].name);
printf("Item code: ");
scanf("%d", &item[i].code);
printf("Quantity: ");
scanf("%d", &item[i].qty);
printf("Price: ");
scanf("%d", &item[i].price);
printf("Manufacturing date (dd mm yyyy): ");
scanf("%d %d %d", &item[i].mfg.day, &item[i].mfg.month, &item[i].mfg.year);
}
printf("<br> ***** INVENTORY *****<br>");
printf("------------------------------------------------------------------<br>");
printf("S.N.| NAME | CODE | QUANTITY | PRICE | MFG.DATE<br>");
printf("------------------------------------------------------------------<br>");
for(i = 0; i < n; i++) {
printf("%-4d%-11s%-6d%-10d%-7d%02d/%02d/%d<br>",
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("------------------------------------------------------------------<br>");
return 0;
}
Enter number of items: 3 Item 1: Item name: pen Item code: 12 Quantity: 50 Price: 25 Manufacturing date (dd mm yyyy): 12 2 2020 Item 2: Item name: pencil Item code: 15 Quantity: 100 Price: 30 Manufacturing date (dd mm yyyy): 11 3 2020 Item 3: Item name: book Item code: 34 Quantity: 30 Price: 60 Manufacturing date (dd mm yyyy): 15 4 2020 ***** INVENTORY ***** ------------------------------------------------------------------ S.N.| NAME | CODE | QUANTITY | PRICE | MFG.DATE ------------------------------------------------------------------ 1 pen 12 50 25 12/02/2020 2 pencil 15 100 30 11/03/2020 3 book 34 30 60 15/04/2020 ------------------------------------------------------------------
Key Points
-
Nested Structures: The
datestructure is nested within thedetailsstructure - Array of Structures: Multiple inventory items are stored using an array of structures
- Data Organization: Related information is grouped logically for better management
- Input Validation: Use proper format specifiers for different data types
Conclusion
Structures provide an elegant solution for inventory management in C by organizing related data efficiently. This approach enables easy data manipulation, storage, and retrieval for complex inventory systems.
