
Chapter- 15: Structures और Unions
📚 C Programming – Complete Course का Roadmap
⬅ Previous Chapter | Current Chapter: Structures और Unions | Next Chapter ➜
Author: Er. Mujtaba Ansari
C Programming में अलग-अलग तरह के Data को एक साथ रखना चाहते हैं?
तो Structures और Unions आपके लिए बहुत उपयोगी हैं। इनकी मदद से आप एक ही जगह पर अलग-अलग Data को आसानी से Store और Manage कर सकते हैं।
Real-Life में इसका उपयोग
- 🎓 Student की जानकारी जैसे Name, Age और Marks Store करना
- 💾 Memory का सही तरीके से उपयोग करना
- 🛒 Product की जानकारी जैसे Name, Price और Quantity Store करना
- 🏦 Bank Account की जानकारी Manage करना
- ⚙️ Software और Embedded Systems में Data Manage करना
- 🏥 Patient की जानकारी Store करना
C Programming Language में Structures और Unions क्या हैं? Syntax, Uses, Programs और Examples के साथ पूरी जानकारी
जब C Programming में छोटे Programs से आगे बढ़कर बड़े और real-world Programs बनाने लगते हैं, तब बहुत सारे Variables को अलग-अलग Manage करना मुश्किल हो सकता है।
मान लीजिए हमें किसी Student की जानकारी Store करनी है:
- Roll Number
- Name
- Marks
- Course
- Contact Details
अगर इन सभी के लिए अलग-अलग Variables बनाए जाएँ, तो Program बड़ा और Manage करना मुश्किल हो सकता है।
यहीं पर Structures और Unions in C Programming बहुत उपयोगी होते हैं।
Structure की मदद से हम अलग-अलग Data Types के Related Data को एक ही नाम के अंदर Group कर सकते हैं। वहीं Union में अलग-अलग Members एक ही Memory Space Share करते हैं।
इस Chapter में हम Structures और Unions को बिल्कुल आसान तरीके से समझेंगे।
इसमें आप सीखेंगे:
- Structure क्या है?
- Structure का Syntax
- Structure Variable कैसे Declare करें?
- Structure Members को Access कैसे करें?
- Structure को Initialize कैसे करें?
- Structure Memory में कैसे Store होता है?
- Structure में Array
- Array of Structures
- Structure को Function में Pass करना
- Function से Structure Return करना
- Nested Structure
- Union क्या है?
- Union कैसे काम करता है?
- Structure और Union में Difference
typedefenum- Structure Pointer और
->Operator - Real-Life Applications
- Common Mistakes
- Practice Programs
- Frequently Asked Questions
Structure क्या है?
Structure in C एक User-Defined Data Type है। इसका उपयोग कई Related Variables को एक ही नाम के अंदर Group करने के लिए किया जाता है।
सबसे खास बात यह है कि Structure में अलग-अलग Data Types के Members भी रख सकते हैं।
उदाहरण के लिए एक Student की जानकारी में:
- Roll Number →
int - Name →
char - Marks →
float
हो सकते हैं।
इन सभी को हम एक ही Student Structure में रख सकते हैं।
आसान Definition:
Structure एक User-Defined Data Type है, जिसका उपयोग Related Data को एक ही Unit में रखने के लिए किया जाता है।
उदाहरण:
struct Student
{
int roll;
char name[20];
float marks;
};
यहाँ Student एक Structure है और roll, name तथा marks इसके Members हैं।
Structure का Syntax
C Programming में Structure का सामान्य Syntax इस प्रकार होता है:
struct structure_name
{
data_type member1;
data_type member2;
data_type member3;
};
उदाहरण:
struct Book
{
int code;
char title[30];
char author[20];
float price;
};
यहाँ:
Book→ Structure का नामcode→ Integer Membertitle→ Character Arrayauthor→ Character Arrayprice→ Float Member
है।
ध्यान दें (Please note.)
Structure Definition के अंत में ; लगाना जरूरी है।
Structure Variable कैसे Declare करें?
Structure Define करने के बाद हमें उसका Variable बनाना होता है।
इसके दो सामान्य तरीके हैं।
Method 1: Structure के साथ ही Variable Declare करना
struct Book
{
int code;
char title[30];
char author[20];
float price;
} book1, book2;
यहाँ book1 और book2 Structure Variables हैं।
Method 2: बाद में Variable Declare करना
पहले Structure Define करें:
struct Book
{
int code;
char title[30];
char author[20];
float price;
};
फिर Variable बनाएं:
struct Book book1;
यह तरीका तब useful होता है जब आप Structure को पहले Define करके बाद में जरूरत के अनुसार उसके Variables बनाना चाहते हैं।
Structure Members को Access कैसे करें?
Structure Variable के Members को Access करने के लिए Dot (.) Operator का उपयोग किया जाता है।
उदाहरण:
book1.code = 101;
book1.price = 499.50;
यहाँ:
book1.code
का मतलब है book1 के अंदर मौजूद code Member को Access करना।
इसी तरह:
book1.price
से price Member को Access किया जाता है।
याद रखें:
Structure Variable + . + Member
variable.member
Structure Initialization in C
Structure Variable को Declare करते समय ही उसकी Values Set की जा सकती हैं।
उदाहरण: Example Program: Structure में Student की Details Store और Display करना
#include <stdio.h>
struct Student
{
int roll;
char name[20];
float marks;
};
int main()
{
struct Student student = {101, "Zain", 89.5};
printf("Roll Number: %d\n", student.roll);
printf("Name: %s\n", student.name);
printf("Marks: %.2f\n", student.marks);
return 0;
}
Output
Roll Number: 101
Name: Zain
Marks: 89.50
यहाँ Values उसी Order में दी गई हैं, जिस Order में Structure Members Declare किए गए हैं।
Structure Memory में कैसे Store होता है?
Structure की एक important बात यह है कि इसके Members को Object के अंदर अपना Storage मिलता है।
उदाहरण:
struct Student
{
int roll;
char name[20];
float marks;
};
इस Structure में:
rollnamemarks
के लिए Storage होता है।
लेकिन Structure का Total Memory Size हमेशा सभी Members के Size का exact जोड़ नहीं होता।
Compiler Memory Alignment के लिए Padding add कर सकता है।
Structure का Actual Size जानने के लिए sizeof का उपयोग किया जा सकता है:
printf("%zu", sizeof(struct Student));
इसलिए Structure का Size Compiler और Target System के अनुसार अलग हो सकता है।
Structure के अंदर Array
Structure के अंदर Array भी बनाया जा सकता है।
यह तब useful होता है जब किसी Record में एक ही Type की कई Values Store करनी हों।
उदाहरण के लिए किसी Student के तीन Subjects के Marks:
struct Student
{
int roll;
char name[20];
int marks[3];
};
यहाँ:
marks[3]
एक Array है जो Structure का Member है।
इसके Elements को इस तरह Access किया जा सकता है:
student.marks[0];
student.marks[1];
student.marks[2];
Array of Structures in C
अगर हमें एक Student की जगह 5 Students का Data Store करना हो, तो 5 अलग-अलग Structure Variables बनाने के बजाय Array of Structures का उपयोग कर सकते हैं।
उदाहरण:
struct Student
{
int roll;
char name[20];
int marks[3];
};
struct Student students[5];
अब students Array में 5 Student Records Store किए जा सकते हैं:
students[0]
students[1]
students[2]
students[3]
students[4]
हर Element एक पूरा Student Structure है।
Array of Structures का Complete Program
Program: Total 5 Students का Data Store और Display करना
OR
Array of Structures का उपयोग करके 5 Students की Details Store और Display करना
#include <stdio.h>
struct Student
{
int roll;
char name[20];
int marks[3];
};
int main()
{
struct Student students[5];
int i, j;
for (i = 0; i < 5; i++)
{
printf("\nEnter details for Student %d\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].roll);
printf("Name: ");
scanf("%19s", students[i].name);
for (j = 0; j < 3; j++)
{
printf("Marks in Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
}
}
printf("\n--- Student Records ---\n");
for (i = 0; i < 5; i++)
{
printf("\nStudent %d\n", i + 1);
printf("Roll Number: %d\n", students[i].roll);
printf("Name: %s\n", students[i].name);
for (j = 0; j < 3; j++)
{
printf("Subject %d: %d\n",
j + 1, students[i].marks[j]);
}
}
return 0;
}
इस प्रकार का Program Student Management जैसे Applications में Multiple Records Handle करने के लिए उपयोगी हो सकता है।
Example Input/Output
Agar user ye details enter kare:
Enter details for Student 1
Roll Number: 101
Name: Zain
Marks in Subject 1: 85
Marks in Subject 2: 90
Marks in Subject 3: 88
Enter details for Student 2
Roll Number: 102
Name: Priya
Marks in Subject 1: 92
Marks in Subject 2: 87
Marks in Subject 3: 95
Enter details for Student 3
Roll Number: 103
Name: Amit
Marks in Subject 1: 78
Marks in Subject 2: 82
Marks in Subject 3: 80
Enter details for Student 4
Roll Number: 104
Name: Neha
Marks in Subject 1: 89
Marks in Subject 2: 91
Marks in Subject 3: 86
Enter details for Student 5
Roll Number: 105
Name: Arjun
Marks in Subject 1: 76
Marks in Subject 2: 84
Marks in Subject 3: 79
To program ka output hoga:
--- Student Records ---
Student 1
Roll Number: 101
Name: Zain
Subject 1: 85
Subject 2: 90
Subject 3: 88
Student 2
Roll Number: 102
Name: Priya
Subject 1: 92
Subject 2: 87
Subject 3: 95
Student 3
Roll Number: 103
Name: Amit
Subject 1: 78
Subject 2: 82
Subject 3: 80
Student 4
Roll Number: 104
Name: Neha
Subject 1: 89
Subject 2: 91
Subject 3: 86
Student 5
Roll Number: 105
Name: Arjun
Subject 1: 76
Subject 2: 84
Subject 3: 79
Structures और Functions
Structure को Functions के साथ भी आसानी से इस्तेमाल किया जा सकता है।
Structure को Function में:
- Argument के रूप में Pass कर सकते हैं।
- Pointer के माध्यम से Pass कर सकते हैं।
- Function से Return कर सकते हैं।
Structure को Function में Pass करना
उदाहरण: Example Program: Structure को Function में Pass करके Student की Details Display करना
#include <stdio.h>
struct Student
{
int roll;
float marks;
};
void display(struct Student student)
{
printf("Roll Number: %d\n", student.roll);
printf("Marks: %.2f\n", student.marks);
}
int main()
{
struct Student student = {101, 92.5};
display(student);
return 0;
}
यहाँ पूरा student Structure Variable display() Function को Pass किया गया है।
Function से Structure Return करना
C में Function किसी Structure को Return भी कर सकता है।
एक अच्छा Example Complex Numbers है।
Complex Number में दो Parts होते हैं:
- Real Part
- Imaginary Part
इन दोनों को Structure में Store किया जा सकता है।
Program: दो Complex Numbers को Add करना
#include <stdio.h>
struct Complex
{
float real;
float imag;
};
struct Complex add(struct Complex a, struct Complex b)
{
struct Complex result;
result.real = a.real + b.real;
result.imag = a.imag + b.imag;
return result;
}
int main()
{
struct Complex first, second, result;
printf("Enter first complex number: ");
scanf("%f %f", &first.real, &first.imag);
printf("Enter second complex number: ");
scanf("%f %f", &second.real, &second.imag);
result = add(first, second);
printf("Result = %.2f + %.2fi\n",
result.real, result.imag);
return 0;
}
यहाँ add() Function दो Structure Variables लेता है और एक Structure Result के रूप में Return करता है।
Structure का उपयोग करके Complex Numbers की Multiplication
दो Complex Numbers:
(a + bi) × (c + di)
का Result:
(ac - bd) + (ad + bc)i
होता है।
Structure की मदद से इसका Function इस तरह बनाया जा सकता है:
struct Complex multiply(struct Complex a, struct Complex b)
{
struct Complex result;
result.real = a.real * b.real - a.imag * b.imag;
result.imag = a.real * b.imag + a.imag * b.real;
return result;
}
इस तरह Structure Mathematical Data को भी व्यवस्थित तरीके से Manage करने में मदद करता है।
Structure से Time Values Add करना
Time को भी Structure में Store किया जा सकता है।
एक Time में:
- Hours
- Minutes
- Seconds
होते हैं।
Complete Program:: Structure का उपयोग करके दो Time Values को Add करना
#include <stdio.h>
struct Time
{
int hours;
int minutes;
int seconds;
};
struct Time addTime(struct Time a, struct Time b)
{
struct Time result;
result.seconds = a.seconds + b.seconds;
result.minutes = a.minutes + b.minutes;
result.hours = a.hours + b.hours;
if (result.seconds >= 60)
{
result.minutes += result.seconds / 60;
result.seconds %= 60;
}
if (result.minutes >= 60)
{
result.hours += result.minutes / 60;
result.minutes %= 60;
}
return result;
}
int main()
{
struct Time first, second, result;
printf("Enter first time (HH MM SS): ");
scanf("%d %d %d",
&first.hours,
&first.minutes,
&first.seconds);
printf("Enter second time (HH MM SS): ");
scanf("%d %d %d",
&second.hours,
&second.minutes,
&second.seconds);
result = addTime(first, second);
printf("Result = %02d:%02d:%02d\n",
result.hours,
result.minutes,
result.seconds);
return 0;
}
Example Input
Enter first time (HH MM SS): 10 45 30
Enter second time (HH MM SS): 05 30 45
Output
Result = 16:16:15
Calculation
- Seconds:
30 + 45 = 75→15 seconds, aur1 minutecarry - Minutes:
45 + 30 + 1 = 76→16 minutes, aur1 hourcarry - Hours:
10 + 5 + 1 = 16 hours
यह Example दिखाता है कि Structure की मदद से ऐसे Data को एक साथ Manage किया जा सकता है जिसमें कई Related Values होती हैं।
Nested Structure क्या है?
जब एक Structure के अंदर दूसरे Structure को Member के रूप में इस्तेमाल किया जाता है, तो उसे Nested Structure कहते हैं।
यह तब उपयोगी होता है जब किसी Real-World Object के अंदर किसी दूसरे Object या Information का अलग Group हो।
उदाहरण के लिए Employee की Information में उसका Address भी हो सकता है।
struct Address
{
char city[30];
int pin;
};
struct Employee
{
int id;
char name[30];
struct Address address;
};
अब City को इस तरह Access कर सकते हैं:
employee.address.city
और PIN Code:
employee.address.pin
Nested Structure का Complete Program
#include <stdio.h>
struct Address
{
char city[30];
int pin;
};
struct Employee
{
int id;
char name[30];
struct Address address;
};
int main()
{
struct Employee employee =
{
101,
"Amit",
{"Delhi", 110001}
};
printf("Employee ID: %d\n", employee.id);
printf("Name: %s\n", employee.name);
printf("City: %s\n", employee.address.city);
printf("PIN Code: %d\n", employee.address.pin);
return 0;
}
Nested Structure complex information को ज्यादा organized तरीके से represent करने में मदद करता है।
Union क्या है?
अब Structure के बाद समझते हैं Union in C Programming।
Union भी एक User-Defined Data Type है, लेकिन इसका Memory Management Structure से अलग होता है।
Union के सभी Members एक ही Memory Location को Share करते हैं।
Union का Syntax
union union_name
{
data_type member1;
data_type member2;
data_type member3;
};
उदाहरण:
union Data
{
int number;
float price;
char name[20];
};
यहाँ number, price और name एक ही shared storage area का उपयोग करते हैं।
Union कैसे काम करता है?
मान लीजिए:
union Data
{
int number;
float price;
};
अब:
union Data data;
अगर हम लिखते हैं:
data.number = 100;
तो shared memory में 100 का Integer representation Store होगा।
इसके बाद अगर हम लिखें:
data.price = 25.5;
तो उसी Memory में price की Value Store होगी।
इसलिए Union में एक Member में नई Value Store करने पर पहले Member की Stored Value का उपयोगी representation replace हो सकता है।
आसान भाषा में
Structure: हर Member के लिए अलग Storage
Union: सभी Members के लिए Shared Storage
Union का Complete Program
#include <stdio.h>
union Data
{
int number;
float price;
};
int main()
{
union Data data;
data.number = 100;
printf("Number: %d\n", data.number);
data.price = 25.5;
printf("Price: %.2f\n", data.price);
return 0;
}
यह Program दिखाता है कि Union के Members एक ही Storage Area Share करते हैं।
Structure और Union में Difference
Structure और Union का सबसे बड़ा Difference उनका Memory Organization है।
| Feature | Structure | Union |
|---|---|---|
| Memory | Members के लिए अलग Storage | Members Shared Storage करते हैं |
| Values | कई Members की Values साथ में रख सकते हैं | आम तौर पर एक समय में एक Member की Stored Representation उपयोग होती है |
| Size | Members और Padding पर Depend करता है | Largest Member और Alignment पर Depend करता है |
| Main Purpose | Related Data को Group करना | Alternative Data Representations के लिए Shared Storage |
| Access | . या -> | . या -> |
Source में भी यही मुख्य Memory Difference बताया गया है।
याद रखने का सबसे आसान तरीका
Structure = Separate Storage
Union = Shared Storage
Structure और Union का Memory Example
Structure:
struct Data
{
int a;
float b;
char c;
};
इसमें प्रत्येक Member का अपना Storage होता है, हालांकि Compiler Alignment के लिए Padding add कर सकता है।
Union:
union Data
{
int a;
float b;
char c;
};
इसमें तीनों Members एक ही Storage Area को Share करते हैं।
यही Structure और Union के Memory Behavior का सबसे महत्वपूर्ण Difference है।
typedef क्या है?
C Programming में typedef का उपयोग किसी Existing Data Type के लिए एक Alias या दूसरा नाम बनाने के लिए किया जाता है।
Syntax
typedef existing_type new_name;
उदाहरण:
typedef int Integer;
अब:
Integer number;
लिखना:
int number;
के बराबर है।
ध्यान रखें कि typedef कोई पूरी तरह नया Primitive Data Type नहीं बनाता। यह Existing Type को एक नया नाम देता है।
typedef के साथ Structure
typedef का उपयोग Structure के साथ बहुत common है।
typedef struct
{
int roll;
char name[20];
float marks;
} Student;
अब Structure Variable बनाने के लिए:
Student student1;
लिख सकते हैं।
इसकी तुलना में बिना typedef के:
struct Student student1;
जैसी Declaration करनी पड़ सकती है।
typedef Structure का Complete Program
#include <stdio.h>
typedef struct
{
int roll;
char name[20];
float marks;
} Student;
int main()
{
Student student = {101, "Rahul", 88.5};
printf("Roll Number: %d\n", student.roll);
printf("Name: %s\n", student.name);
printf("Marks: %.2f\n", student.marks);
return 0;
}
typedef Structure Declaration को छोटा और पढ़ने में आसान बना सकता है।
enum क्या है?
enum का पूरा नाम Enumeration है।
यह एक User-Defined Type है जिसका उपयोग Named Integer Constants का Set बनाने के लिए किया जाता है।
Syntax
enum enum_name
{
constant1,
constant2,
constant3
};
उदाहरण:
enum Color
{
Black,
Red,
Blue,
Green
};
Default रूप से Enumeration Constants की शुरुआत 0 से होती है:
Black = 0
Red = 1
Blue = 2
Green = 3
enum में Custom Values
आप enum में अपनी Values भी Set कर सकते हैं।
enum Color
{
Black,
Red,
Blue = 5,
Green
};
इसमें Values होंगी:
Black = 0
Red = 1
Blue = 5
Green = 6
एक Value को Explicit रूप से Set करने के बाद अगली Enumeration Values आगे के Number से Continue होती हैं।
enum का Example
#include <stdio.h>
enum Status
{
FALSE,
TRUE
};
int main()
{
enum Status result = TRUE;
printf("Status: %d\n", result);
return 0;
}
Output
Status: 1
Named Constants का उपयोग Code को ज्यादा Meaningful बनाने में मदद कर सकता है।
Structure Pointer क्या है?
Structure के साथ Pointer का उपयोग भी किया जा सकता है।
उदाहरण:
struct Student
{
int roll;
float marks;
};
अब:
struct Student student;
struct Student *ptr = &student;
यहाँ ptr एक Structure Pointer है।
Structure Pointer से Member Access करने के लिए Arrow (->) Operator का उपयोग किया जाता है।
ptr->roll = 101;
ptr->marks = 90.5;
यह नीचे दिए गए Code के बराबर है:
(*ptr).roll = 101;
(*ptr).marks = 90.5;
याद रखें:
Normal Structure Variable:
student.roll
Structure Pointer:
ptr->roll
Structures और Unions के Real-Life Applications
Structures और Unions केवल Exam या Practice Programs तक सीमित नहीं हैं। इनका उपयोग अलग-अलग प्रकार के Software और System Programs में Data को Organize करने के लिए किया जाता है।
1. Student Management System
Student का Data:
Roll Number
Name
Course
Marks
Contact Details
एक Structure में रखा जा सकता है।
2. Banking Applications
Bank Account में:
Account Number
Customer Name
Balance
Account Type
जैसी Information हो सकती है।
3. E-Commerce System
Product के लिए:
Product ID
Product Name
Price
Quantity
Category
Store किया जा सकता है।
4. Employee Management
Employee Record में:
Employee ID
Name
Salary
Department
Address
रखा जा सकता है।
5. Hospital Management
Patient Record में:
Patient ID
Name
Age
Doctor
Medical Record Information
जैसी Information हो सकती है।
6. Games और Applications
एक Game में Player की Information:
Player Name
Score
Level
Position
Health
Structure के अंदर रखी जा सकती है।
7. Embedded और System Programming
Structures का उपयोग कई Fields को एक साथ Organize करने के लिए किया जा सकता है, जबकि Unions का उपयोग तब किया जा सकता है जब अलग-अलग Data Representations को Shared Storage में रखना हो।
C Programming में Structures और Unions क्यों सीखें?
Structures और Unions C Programming में Data Handling को ज्यादा organized बनाने में मदद करते हैं।
इन्हें सीखने के बाद आप:
- Real-World Objects को Program में Represent कर सकते हैं।
- Related Data को एक साथ Group कर सकते हैं।
- Multiple Records Manage कर सकते हैं।
- Array of Structures का उपयोग कर सकते हैं।
- Structure को Function में Pass कर सकते हैं।
- Function से Structure Return कर सकते हैं।
- Nested Data Models बना सकते हैं।
- Union के Shared Memory Concept को समझ सकते हैं।
typedefसे Code को आसान बना सकते हैं।enumसे Named Constants बना सकते हैं।- आगे Pointers, Dynamic Memory और Data Structures जैसे Topics समझने के लिए Foundation तैयार कर सकते हैं।
Beginners की Common Mistakes
Structures और Unions सीखते समय Beginners अक्सर कुछ छोटी गलतियाँ करते हैं।
1. Structure के बाद Semicolon भूलना
गलत:
struct Student
{
int roll;
}
सही:
struct Student
{
int roll;
};
Structure Definition के बाद ; लगाना जरूरी है।
2. गलत Member Access Operator
Normal Structure Variable के साथ:
student.roll
Structure Pointer के साथ:
studentPtr->roll
का उपयोग करें।
3. Union को Structure समझना
Union के Members का Storage अलग-अलग नहीं होता।
वे एक ही Storage Area Share करते हैं।
4. gets() का उपयोग करना
पुराने C Programs में कभी-कभी:
gets(name);
देखने को मिलता है।
लेकिन gets() Input की Length को Safely Limit नहीं करता। इसलिए इसे Avoid करना चाहिए। जरूरत के अनुसार fgets() जैसी safer input technique का उपयोग किया जा सकता है।
5. sizeof(struct) को केवल Members के Size का जोड़ समझना
कई Beginners सोचते हैं कि Structure का Size हमेशा उसके सभी Members के Size के बराबर होगा।
ऐसा जरूरी नहीं है।
Compiler Memory Alignment के लिए Padding Add कर सकता है।
Structure और Union – Quick Revision
| Concept | आसान मतलब |
|---|---|
| Structure | Related Data को एक Type में Group करता है |
| Structure Member | Structure के अंदर Declare किया गया Variable |
. Operator | Structure Object के Member को Access करता है |
-> Operator | Structure Pointer के Member को Access करता है |
| Array of Structures | Multiple Structure Records Store करता है |
| Nested Structure | एक Structure के अंदर दूसरा Structure |
| Union | Members Shared Storage करते हैं |
typedef | Existing Type का दूसरा नाम बनाता है |
enum | Named Integer Constants Define करता है |
यह पूरा revision Structure, Union, typedef, enum और Member Access के मुख्य Concepts को जल्दी दोहराने में मदद करता है।
Structure और Union को Simple एक आसान Example से समझें
मान लीजिए आपके पास एक Student है:
Roll Number
Name
Marks
अगर आपको तीनों Values एक साथ Store करनी हैं, तो Structure useful है।
struct Student
{
int roll;
char name[20];
float marks;
};
अब अगर आपके पास ऐसी Situation है जहाँ अलग-अलग समय पर अलग-अलग Type की Value Store करनी है और Shared Memory उपयोग करनी है, तो Union उपयोगी हो सकता है।
इसलिए आसान भाषा में:
Structure का उपयोग Related Data को साथ रखने के लिए किया जाता है।
Union का उपयोग Shared Memory में Alternative Data Representations रखने के लिए किया जाता है।
Conclusion:
Structures और Unions in C Programming Data को Organize और Manage करने के लिए बहुत important concepts हैं।
Structure की मदद से हम अलग-अलग Data Types के Related Members को एक ही Unit में रख सकते हैं। Array of Structures की मदद से Multiple Records Manage किए जा सकते हैं और Nested Structures की मदद से Complex Data को भी व्यवस्थित किया जा सकता है।
दूसरी तरफ, Union में सभी Members एक ही Storage Area Share करते हैं। इसलिए इसका Memory Behavior Structure से अलग होता है।
इसके साथ typedef Code में Data Types के लिए आसान नाम बनाने में मदद करता है और enum Named Integer Constants को Define करने का तरीका देता है।
Structures, Unions, typedef, enum और Structure Pointers को अच्छी तरह समझना C Programming के आगे के Advanced Topics के लिए एक मजबूत Foundation तैयार करता है।
Frequently Asked Questions (FAQs)
1. C Programming में Structure क्या है?
Structure एक User-Defined Data Type है जिसका उपयोग Related Variables को एक ही Unit में Group करने के लिए किया जाता है।
2. C में Union क्या है?
Union एक User-Defined Data Type है जिसमें सभी Members एक ही Memory Storage को Share करते हैं।
3. Structure और Union में मुख्य Difference क्या है?
मुख्य Difference Memory का है।
Structure में Members के लिए अलग Storage होता है, जबकि Union में Members Shared Storage का उपयोग करते हैं।
4. क्या Structure के अंदर Array बनाया जा सकता है?
हाँ। Structure के अंदर Array को Member के रूप में Declare किया जा सकता है।
उदाहरण:
struct Student
{
int marks[3];
};
5. क्या Structure को Function में Pass कर सकते हैं?
हाँ। Structure को Function में Value के रूप में या Pointer के माध्यम से Pass किया जा सकता है।
6. क्या Function Structure Return कर सकता है?
हाँ। C Programming में Function Structure Value Return कर सकता है।
7. Nested Structure क्या होता है?
जब एक Structure के अंदर दूसरे Structure को Member के रूप में इस्तेमाल किया जाता है, तो उसे Nested Structure कहते हैं।
8. typedef का उपयोग क्यों किया जाता है?
typedef किसी Existing Data Type का एक नया Alias या नाम बनाने के लिए उपयोग किया जाता है।
9. enum क्या है?
enum का उपयोग Named Integer Constants का Set Define करने के लिए किया जाता है।
10. Structure Member को Access करने के लिए कौन-सा Operator उपयोग होता है?
Normal Structure Variable के लिए:
.
और Structure Pointer के लिए:
->
Operator का उपयोग किया जाता है।

मुजतबा अंसारी एक सॉफ्टवेयर इंजीनियर तथा The Tech Earth के संस्थापक (Founder) और CEO हैं। वे तकनीक, आर्टिफिशियल इंटेलिजेंस (AI), सॉफ्टवेयर, साइबर सुरक्षा, ब्लॉगिंग, हेल्थ टेक, एग्री टेक और डिजिटल ट्रेंड्स से जुड़े विषयों पर लेख लिखते हैं। उनका उद्देश्य पाठकों को नई तकनीकों और डिजिटल दुनिया की नवीनतम जानकारियों से अपडेट रखना है।
