C Program to Store and Display Records of N Products Using Union ( यूनियन का उपयोग करके N प्रोडक्ट के रिकॉर्ड स्टोर और प्रदर्शित करने का C प्रोग्राम)

In C language, an array of unions can be used to store data for multiple products. A union allows all its data members to share the same memory location. In this program, we store and display the prices of N products using an array of unions.

C भाषा में Array of Union का उपयोग करके कई products का डेटा स्टोर किया जा सकता है। Union के सभी data members एक ही memory location साझा करते हैं। इस प्रोग्राम में Array of Union का उपयोग करके N products की कीमतें स्टोर और प्रदर्शित की जाती हैं।

C Program

#include <stdio.h>
#define N 5
union product
{
int pid;
char pname[20];
char mfd[11];
char sname[20];
float price;
};
int main(void)
{
int i;
union product p[N];
// Input product prices
for (i = 0; i < N; i++)
{
printf("Enter price of product %d: ", i + 1);
scanf("%f", &p[i].price);
}
// Display product prices
printf("\nProduct Prices:\n");
for (i = 0; i < N; i++)
{
printf("Price of product %d = %.2f\n",
i + 1, p[i].price);
}
return 0;
}

Output

Enter price of product 1: 150.50
Enter price of product 2: 250.00
Enter price of product 3: 99.75
Enter price of product 4: 500.00
Enter price of product 5: 350.25
Product Prices:
Price of product 1 = 150.50
Price of product 2 = 250.00
Price of product 3 = 99.75
Price of product 4 = 500.00
Price of product 5 = 350.25

Explanation (व्याख्या)

  1. #define N 5 defines the number of products.

    #define N 5 products की संख्या निर्धारित करता है।

  2. The product union contains five members: pid, pname, mfd, sname and price.

    product union में पाँच data members हैं।

  3. union product p[N] declares an array of five union variables.

    यह पाँच union variables का array बनाता है।

  4. The first for loop accepts the prices of five products.

    पहला for loop पाँच products की कीमतें इनपुट करता है।

  5. The second for loop displays the stored product prices.

    दूसरा for loop स्टोर की गई product prices प्रदर्शित करता है।

Important Points

  • All members of a union share the same memory location.

  • Each union variable can hold one member's value at a time.

  • This program stores only product prices, not all product details simultaneously.

  • To store product ID, name, manufacturing date, supplier name and price together, use struct instead of union.

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025