Structure in C language

Introduction

अब तक हमने variables और arrays के बारे में सीखा, जहाँ हम:

  • एक variable में एक value store करते हैं
  • array में same type की multiple values store करते हैं

लेकिन real-world problems में हमें कई बार different data types को एक साथ store करना होता है, जैसे:

Student record:

  • Name (string)
  • Roll number (int)
  • Marks (float)

अब problem यह है कि:

  • array सिर्फ same type का data store करता है
  • अलग-अलग variables बनाना messy हो जाता है

इसी समस्या को solve करने के लिए C language में Structure का concept दिया गया है।

Structure हमें allow करता है:

  • different data types को एक single unit में store करने के लिए
  • real-world entities को represent करने के लिए

इसलिए structure को user-defined data type भी कहा जाता है।

Definition

Structure एक user-defined data type है जो different data types के variables को एक single unit में group करता है।

Structure Declaration

Syntax

struct structure_name
{
data_type member1;
data_type member2;
...
};

Example

struct student
{
int roll;
char name[20];
float marks;
};

Explanation:

  • student → structure name
  • roll, name, marks → members

Structure Variable Declaration

struct student s1;

Accessing Structure Members

dot operator (.) का उपयोग होता है

s1.roll = 1;
s1.marks = 85.5;

Example 1: Basic Structure Program

#include <stdio.h>
struct student
{
int roll;
float marks;
};
int main()
{
struct student s1;

s1.roll = 1;
s1.marks = 90.5;

printf("Roll: %d\n", s1.roll);
printf("Marks: %.1f", s1.marks);

return 0;
}

Output:

Roll: 1
Marks: 90.5

Explanation:

  • structure variable बनाया
  • values assign की
  • print किया

Example 2: Multiple Structure Variables

#include <stdio.h>
struct student
{
int roll;
float marks;
};
int main()
{
struct student s1 = {1, 85.5};
struct student s2 = {2, 90.0};

printf("%d %.1f\n", s1.roll, s1.marks);
printf("%d %.1f", s2.roll, s2.marks);

return 0;
}

Output:

1 85.5
2 90.0

Example 3: Input from User

#include <stdio.h>
struct student
{
int roll;
float marks;
};
int main()
{
struct student s;

printf("Enter roll and marks: ");
scanf("%d %f", &s.roll, &s.marks);

printf("Roll: %d Marks: %.1f", s.roll, s.marks);

return 0;
}

Example 4: Structure with String

#include <stdio.h>
struct student
{
int roll;
char name[20];
};
int main()
{
struct student s1 = {1, "Rahul"};

printf("%d %s", s1.roll, s1.name);

return 0;
}

Output:

1 Rahul

Technical Understanding

Structure का use इन situations में होता है:

  • student record system
  • employee database
  • real-world entities representation
  • mixed data handling

Important Points

  • Structure user-defined data type है
  • Different data types store कर सकता है
  • Dot operator से access होता है
  • Array of structures भी बना सकते हैं

Exam Points

  • Structure = different data types का group
  • Syntax बहुत important है
  • Dot operator (.) याद रखें
  • Practical exam में structure programs आते हैं
  • Input/output based programs important हैं

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top