संरचना एक उपयोगकर्ता-परिभाषित डेटा प्रकार है, जिसका उपयोग विभिन्न डेटा प्रकारों के डेटा के संग्रह को संग्रहीत करने के लिए किया जाता है।
संरचना एक सरणी के समान है। फर्क सिर्फ इतना है कि एक सरणी का उपयोग समान डेटा प्रकारों को संग्रहीत करने के लिए किया जाता है जबकि संरचना का उपयोग विभिन्न डेटा प्रकारों को संग्रहीत करने के लिए किया जाता है।
कीवर्ड struct संरचना घोषित करने के लिए है।
संरचना के अंदर के वेरिएबल संरचना के सदस्य हैं।
एक संरचना को निम्नानुसार घोषित किया जा सकता है:
Struct structurename{
//member declaration
};
उदाहरण
निम्नलिखित C प्रोग्राम संरचना वेरिएबल तक पहुँचने के लिए है:
struct book{
int pages;
float price;
char author[20];
};
Accessing structure members in C
#include<stdio.h>
//संरचना घोषित करना//
struct{
char name[50];
int roll;
float percentage;
char grade[50];
}s1,s2;
void main(){
//यूजर इनपुट पढ़ना//
printf("enter Name of 1st student : ");
gets(s1.name);
printf("enter Roll number of 1st student : ");
scanf("%d",&s1.roll);
printf("Enter the average of 1st student : ");
scanf("%f",&s1.percentage);
printf("Enter grade status of 1st student : ");
scanf("%s",s1.grade);
//आउटपुट प्रिंट करना//
printf("The name of 1st student is : %s\n",s1.name);
printf("The roll number of 1st student is : %d\n",s1.roll);
printf("The average of 1st student is : %f\n",s1.percentage);
printf("The student 1 grade is : %s and percentage of %f\n",s1.grade,s1.percentage);
}
आउटपुट
जब उपरोक्त प्रोग्राम निष्पादित होता है, तो यह निम्नलिखित परिणाम उत्पन्न करता है:
enter Name of 1st student: Bhanu enter Roll number of 1st student: 2 Enter the average of 1st student: 68 Enter grade status of 1st student: A The name of 1st student is: Bhanu The roll number of 1st student is: 2 The average of 1st student is: 68.000000 The student 1 grade is: A and percentage of 68.000000