Structures in C are used to group different variables under one name. Here we use a structure to represent an employee with a name, age, and salary.
C में structures का उपयोग अलग-अलग डेटा को एक साथ जोड़ने के लिए किया जाता है। यहाँ हम एक employee को represent करने के लिए structure का उपयोग कर रहे हैं जिसमें नाम, उम्र, और सैलरी शामिल हैं।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp1 = {"Aryan Gangwar", 25, 45000.50};
printf("Name: %s\n", emp1.name);
printf("Age: %d\n", emp1.age);
printf("Salary: %.2f\n", emp1.salary);
return 0;
}
Output:
Name: Aryan Gangwar Age: 25 Salary: 45000.50
Defines a structure Employee and initializes its fields.
Employee structure define करके उसके fields को initialize किया।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp;
printf("Enter name: ");
fgets(emp.name, sizeof(emp.name), stdin);
printf("Enter age: ");
scanf("%d", &emp.age);
printf("Enter salary: ");
scanf("%f", &emp.salary);
printf("\nEmployee Details:\n");
printf("Name: %s", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
Output (example input):
Enter name: Aryan Gangwar Enter age: 25 Enter salary: 45000.50 Employee Details: Name: Aryan Gangwar Age: 25 Salary: 45000.50
Takes user input for employee details and displays them.
यूजर से डेटा लेकर employee के details दिखाता है।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp[2] = {
{"Aryan Gangwar", 25, 45000.50},
{"Riya Sharma", 28, 52000.75}
};
for(int i = 0; i < 2; i++) {
printf("Employee %d:\n", i+1);
printf("Name: %s\n", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Salary: %.2f\n\n", emp[i].salary);
}
return 0;
}
Output:
Employee 1: Name: Aryan Gangwar Age: 25 Salary: 45000.50 Employee 2: Name: Riya Sharma Age: 28 Salary: 52000.75
Array of structures to store multiple employees.
एक से ज्यादा employees को array of structures में स्टोर करना।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
void printEmployee(struct Employee emp) {
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
}
int main() {
struct Employee emp1 = {"Aryan Gangwar", 25, 45000.50};
printEmployee(emp1);
return 0;
}
Output:
Name: Aryan Gangwar Age: 25 Salary: 45000.50
Pass structure as function argument to print details.
structure को function में argument के रूप में पास करके details print करना।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp1 = {"Aryan Gangwar", 25, 45000.50};
struct Employee *ptr = &emp1;
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Salary: %.2f\n", ptr->salary);
return 0;
}
Output:
Name: Aryan Gangwar Age: 25 Salary: 45000.50
Using pointer to structure and arrow operator to access members.
structure pointer और arrow operator से members को एक्सेस करना।
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Employee {
char name[50];
int age;
float salary;
struct Date joiningDate;
};
int main() {
struct Employee emp = {"Aryan Gangwar", 25, 45000.50, {15, 7, 2020}};
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
printf("Joining Date: %02d-%02d-%d\n", emp.joiningDate.day, emp.joiningDate.month, emp.joiningDate.year);
return 0;
}
Output:
Name: Aryan Gangwar Age: 25 Salary: 45000.50 Joining Date: 15-07-2020
Structure inside another structure to store joining date.
Structure के अंदर structure का उपयोग करके joining date को store करना।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp[3];
float totalSalary = 0;
for(int i = 0; i < 3; i++) {
printf("Enter details for Employee %d\n", i+1);
printf("Name: ");
getchar(); // consume leftover newline
fgets(emp[i].name, sizeof(emp[i].name), stdin);
printf("Age: ");
scanf("%d", &emp[i].age);
printf("Salary: ");
scanf("%f", &emp[i].salary);
totalSalary += emp[i].salary;
}
printf("\nEmployee Details:\n");
for(int i = 0; i < 3; i++) {
printf("Employee %d: %sAge: %d, Salary: %.2f\n", i+1, emp[i].name, emp[i].age, emp[i].salary);
}
printf("\nAverage Salary = %.2f\n", totalSalary/3);
return 0;
}
Input multiple employees and calculate average salary.
कई employees के डेटा इनपुट करें और average salary निकालें।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
struct Employee createEmployee(char *name, int age, float salary) {
struct Employee emp;
strcpy(emp.name, name);
emp.age = age;
emp.salary = salary;
return emp;
}
int main() {
struct Employee emp1 = createEmployee("Aryan Gangwar", 25, 45000.50);
printf("Name: %s\n", emp1.name);
printf("Age: %d\n", emp1.age);
printf("Salary: %.2f\n", emp1.salary);
return 0;
}
Function returns a structure after creating it.
Function structure बनाकर उसे return करता है।
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
void updateSalary(struct Employee *emp, float newSalary) {
emp->salary = newSalary;
}
int main() {
struct Employee emp1 = {"Aryan Gangwar", 25, 45000.50};
updateSalary(&emp1, 50000.75);
printf("Updated Salary: %.2f\n", emp1.salary);
return 0;
}
Update structure member using pointer inside a function.
Pointer से structure के member को function में update करना।
#include <stdio.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp1 = {"Aryan Gangwar", 25, 45000.50};
printf("Name: %s\n", emp1.name);
printf("Age: %d\n", emp1.age);
printf("Salary: %.2f\n", emp1.salary);
return 0;
}
Use typedef to avoid writing 'struct' repeatedly.
typedef से 'struct' लिखने की जरूरत नहीं होती।
Write a program to create an array of 3 employees. Input their details from the user and then print all employee details. Also calculate the average age and salary.
3 employees का array बनाएं। उनके details यूजर से इनपुट लें और फिर सभी employees के details प्रिंट करें। साथ में average age और salary भी निकालें।