C Program to Store and Display Records of N Students Using Union (यूनियन का उपयोग करके N स्टूडेंट्स के रिकार्ड्स स्टोर और डिस्प्ले करना)

In C language, an array of unions can be used to store multiple values using a common memory area. In this program, a union is used to store and display the names of 10 students.

C Language में Multiple Values को एक Common Memory Area में Store करने के लिए Array of Unions का उपयोग किया जा सकता है। इस Program में Union की सहायता से 10 Students के Names Store और Display किए गए हैं।

C Program

#include <stdio.h>
#include <string.h>
#define N 10
union student
{
int rollno;
char name[20];
char gender;
float fees;
};
int main()
{
int i;
union student s[N];
// Input student names
for (i = 0; i < N; i++)
{
printf("Enter name of student %d: ", i + 1);
fgets(s[i].name, sizeof(s[i].name), stdin);
s[i].name[strcspn(s[i].name, "\n")] = '\0';
}
// Display student names
printf("\n========== STUDENT NAMES ==========\n");
for (i = 0; i < N; i++)
{
printf("Name of student %d = %s\n", i + 1, s[i].name);
}
return 0;
}

Sample Output

Enter name of student 1: Rahul
Enter name of student 2: Priya
Enter name of student 3: Amit
========== STUDENT NAMES ==========
Name of student 1 = Rahul
Name of student 2 = Priya
Name of student 3 = Amit
... Names of remaining students ...

Note: The program accepts and displays names of all 10 students.

Explanation

  1. Union Declaration: union student contains four members: rollno, name, gender and fees.

    union student में चार Members Declare किए गए हैं।

  2. Array of Unions: union student s[N]; creates an array of 10 union variables.

    यह Statement 10 Union Variables का Array बनाता है।

  3. Input: The first for loop accepts the names of students using fgets().

    पहला for Loop fgets() की सहायता से Students के Names Input करता है।

  4. Display: The second for loop displays the stored student names.

    दूसरा for Loop Store किए गए Students के Names Display करता है।

Important Points

  • A union can contain different data types as its members.

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

  • The size of a union is generally determined by its largest member, including alignment requirements.

  • In this program, only the name member is used for each union variable.

महत्वपूर्ण बिंदु:

  • Union में अलग-अलग Data Types के Members हो सकते हैं।

  • Union के सभी Members एक ही Memory Location Share करते हैं।

  • Union का Size सामान्यतः उसके सबसे बड़े Member और Memory Alignment पर निर्भर करता है।

  • इस Program में प्रत्येक Union Variable के लिए केवल name Member का उपयोग किया गया है।

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