C Language Structure: Definition, Syntax और Real Examples
अगर आप C Programming सीख रहे हैं और सोच रहे हैं, "Structure in C क्या है?", तो आप सही जगह आए हैं। इस पोस्ट में हम सी लैंग्वेज में स्ट्रक्चर को आसान भाषा में समझेंगे, साथ ही Syntax, Examples, Nested Structures, और Structure vs Union जैसे जरूरी concepts को cover करेंगे।
👉 Structure को examples के साथ अच्छे से समझना चाहते हैं, तो इस पोस्ट को पूरी तरह पढ़ें, notes बनाएं और 📌 Bookmark करना बिल्कुल न भूलें!
Contents
- C Structure का Definition
- Structure Syntax in C
- C Structure Example
- Nested Structure in C
- Array of Structures
- Structure vs Union
- FAQ
C Structure का Definition
सी प्रोग्रामिंग में Structure एक user-defined data type है, जो एक साथ कई अलग-अलग data types के variables को store करने के लिए इस्तेमाल किया जाता है। उदाहरण के लिए, किसी student के लिए name, roll number, और marks को एक साथ manage करना।
Structure का मुख्य फायदा यह है कि आप अलग-अलग data को logically group कर सकते हैं और memory को efficiently use कर सकते हैं। यह C language के user defined data type का सबसे practical example है।
Examples of C Structures
1. Structure with Functions (Passing Structure to Functions)
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
// Function to print student details
void printStudent(struct Student s) {
printf("Name: %s\n", s.name);
printf("Roll: %d\n", s.roll);
printf("Marks: %.2f\n", s.marks);
}
int main() {
struct Student s1 = {"Anjali", 102, 88.5};
printStudent(s1);
return 0;
}
इस उदाहरण में हमने structure को function में pass किया। यह interview में अक्सर पूछा जाता है।
2. Structure Array with Loops
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
struct Employee emp[3] = {
{"Ravi", 201, 50000},
{"Neha", 202, 55000},
{"Amit", 203, 60000}
};
for(int i = 0; i < 3; i++) {
printf("%s (ID: %d) - Salary: %.2f\n", emp[i].name, emp[i].id, emp[i].salary);
}
return 0;
}
3. Nested Structure with Array
struct Date {
int day;
int month;
int year;
};
struct Employee {
char name[50];
int id;
struct Date joiningDate;
};
struct Employee emp[2] = {
{"Sunil", 301, {12, 5, 2020}},
{"Kavita", 302, {23, 8, 2021}}
};
Nested structures combined with arrays create complex but manageable data models.
Common Interview Questions on C Structures
- Structure और Union में क्या अंतर है?
- Structure को function में pass कैसे किया जाता है?
- Nested Structure क्या होता है और कब उपयोग होता है?
- Array of Structures का real-life example क्या है?
- Structure padding और memory alignment क्या है?
- Bit-fields in Structures क्या होते हैं?
Real-Life Project Use-Cases of C Structures
1. Student Management System
आप student के name, roll number, marks, और DOB को store करने के लिए structures का use कर सकते हैं। Structure के array के साथ आप सभी students के records को efficiently manage कर सकते हैं।
2. Employee Payroll System
Employee का नाम, ID, Salary, और Joining Date को structures में organize किया जा सकता है। इसके साथ arithmetic operations करके salary calculations और deductions manage किए जा सकते हैं।
3. Inventory Management System
Products के लिए structure बनाकर Name, Product ID, Price, Quantity store किया जा सकता है। Array of structures से store में stock आसानी से track किया जा सकता है।
4. Game Score Tracker
Players के लिए structure में Name, Score, Level store कर सकते हैं। Nested structure का use करके player achievements और inventory items track कर सकते हैं।
5. Hospital Patient Record System
Patient details जैसे Name, Age, Blood Group, Medical History, और Appointment Date structures में organize किए जा सकते हैं। Complex data के लिए nested structures और arrays का use किया जा सकता है।
इन examples और use-cases से students और beginners को C Programming Structure का real-world importance और practical application समझ में आता है।
Structure Syntax in C
Structure define करने का basic syntax इस प्रकार है:
struct StructureName {
dataType member1;
dataType member2;
...
};
उदाहरण:
struct Student {
char name[50];
int roll;
float marks;
};
यहाँ struct Student एक structure type है, और इसमें name, roll, marks members हैं।
C Structure Example
Structure का practical use कुछ इस तरह दिखता है:
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
struct Student s1;
strcpy(s1.name, "Rahul");
s1.roll = 101;
s1.marks = 95.5;
printf("Name: %s\n", s1.name);
printf("Roll: %d\n", s1.roll);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
Output:
- Name: Rahul
- Roll: 101
- Marks: 95.50
Nested Structure in C
Structure के अंदर structure भी रख सकते हैं, जिसे Nested Structure कहते हैं। उदाहरण:
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int roll;
struct Date dob; // Nested Structure
};
Nested structures को memory efficiently organize करने और complex data को manage करने के लिए use किया जाता है।
Array of Structures
Structure के array का use एक जैसी type की multiple entities को store करने के लिए किया जाता है।
struct Student s[3]; // 3 students का array
फिर आप loop के साथ सभी students के details को access कर सकते हैं।
Structure vs Union
Structure और Union दोनों user-defined data types हैं, लेकिन memory allocation में फर्क है।
- Structure: सभी members को अलग-अलग memory मिलती है।
- Union: सभी members same memory share करते हैं।
- इसलिए structure ज्यादा flexible है, लेकिन union memory efficient है।
FAQ (Frequently Asked Questions)
Q1: C Structure क्या है?
A: C Structure एक user-defined data type है, जो अलग-अलग data types के variables को एक साथ store करने के लिए use होता है।
Q2: Structure और Union में क्या अंतर है?
A: Structure में सभी members को अलग memory मिलती है, जबकि Union में सभी members same memory share करते हैं।
Q3: Nested Structure क्या होता है?
A: Nested Structure वह structure है जो किसी अन्य structure के अंदर define किया जाता है।
Q4: C Structure के real-life examples क्या हैं?
A: Student records, Employee details, Product catalogues, और Game player stats structure के real-life examples हैं।
Q5: Array of Structures क्यों इस्तेमाल करते हैं?
A: Array of Structures का use multiple similar type entities को store और manage करने के लिए किया जाता है।
Post a Comment
Blogger FacebookYour Comment Will be Show after Approval , Thanks