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 10union student{int rollno;char name[20];char gender;float fees;};int main(){int i;union student s[N];// Input student namesfor (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 namesprintf("\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: RahulEnter name of student 2: PriyaEnter name of student 3: Amit========== STUDENT NAMES ==========Name of student 1 = RahulName of student 2 = PriyaName of student 3 = Amit... Names of remaining students ...
Note: The program accepts and displays names of all 10 students.
Explanation
Union Declaration:
union studentcontains four members:rollno,name,genderandfees.union studentमें चार Members Declare किए गए हैं।Array of Unions:
union student s[N];creates an array of 10 union variables.यह Statement 10 Union Variables का Array बनाता है।
Input: The first
forloop accepts the names of students usingfgets().पहला
forLoopfgets()की सहायता से Students के Names Input करता है।Display: The second
forloop displays the stored student names.दूसरा
forLoop 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
namemember is used for each union variable.
महत्वपूर्ण बिंदु:
Union में अलग-अलग Data Types के Members हो सकते हैं।
Union के सभी Members एक ही Memory Location Share करते हैं।
Union का Size सामान्यतः उसके सबसे बड़े Member और Memory Alignment पर निर्भर करता है।
इस Program में प्रत्येक Union Variable के लिए केवल
nameMember का उपयोग किया गया है।
Comments
Post a Comment