In C programming, a structure (or struct
) is a user-defined data type that allows grouping variables of different types under a single name. This is useful for organizing and managing related data efficiently. Structures are particularly useful when you need to model complex data that consists of various attributes.
Why Use Structures?
- Grouping Related Data: Structures enable you to group related data of different types into a single unit. For example, if you need to manage data about a student, you can group their name, age, and grades together in one structure.
- Improved Code Readability: By using structures, your code becomes more readable and easier to understand. You can refer to a single structure to access all related data instead of dealing with multiple separate variables.
- Easier Data Management: Structures simplify the management of related data, especially when dealing with arrays of structures or passing structured data to functions.
- Memory Efficiency: Structures allow efficient use of memory by ensuring that related data is stored together, which can also improve performance due to better cache utilization.
Defining a Structure
struct Student {
char name[50];
int age;
float GPA;
};
Questions on Structure
WAP in C to define a simple structure and print its members.
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student s1 = {"John Doe", 20};
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
return 0;
}
WAP in C to take user input for a structure and print its members.
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student s1;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter age: ");
scanf("%d", &s1.age);
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
return 0;
}
WAP in C to create an array of structures and print the details.
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student students[3];
for (int i = 0; i < 3; i++) {
printf("Enter name of student %d: ", i+1);
scanf("%s", students[i].name);
printf("Enter age of student %d: ", i+1);
scanf("%d", &students[i].age);
}
for (int i = 0; i < 3; i++) {
printf("Student %d: Name: %s, Age: %d\n", i+1, students[i].name, students[i].age);
}
return 0;
}
WAP in C to pass a structure to a function and display its members.
#include <stdio.h>
struct Student {
char name[50];
int age;
};
void display(struct Student s) {
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
}
int main() {
struct Student s1 = {"John Doe", 20};
display(s1);
return 0;
}
WAP in C to return a structure from a function.
#include <stdio.h>
struct Student {
char name[50];
int age;
};
struct Student getStudent() {
struct Student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter age: ");
scanf("%d", &s.age);
return s;
}
int main() {
struct Student s1 = getStudent();
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
return 0;
}
WAP in C to calculate the total and average marks of a student using structures.
#include <stdio.h>
struct Student {
char name[50];
int marks[3];
};
int main() {
struct Student s1;
int total = 0;
float average;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter marks for 3 subjects: ");
for (int i = 0; i < 3; i++) {
scanf("%d", &s1.marks[i]);
total += s1.marks[i];
}
average = total / 3.0;
printf("Total: %d\n", total);
printf("Average: %.2f\n", average);
return 0;
}
WAP in C to store and print information of multiple employees using structures.
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
struct Employee employees[3];
for (int i = 0; i < 3; i++) {
printf("Enter name of employee %d: ", i+1);
scanf("%s", employees[i].name);
printf("Enter ID of employee %d: ", i+1);
scanf("%d", &employees[i].id);
printf("Enter salary of employee %d: ", i+1);
scanf("%f", &employees[i].salary);
}
for (int i = 0; i < 3; i++) {
printf("Employee %d: Name: %s, ID: %d, Salary: %.2f\n", i+1, employees[i].name, employees[i].id, employees[i].salary);
}
return 0;
}
WAP in C to implement a student database using structures and perform operations like add, delete, and search.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
};
void addStudent(struct Student students[], int *n, struct Student s) {
students[*n] = s;
(*n)++;
}
void deleteStudent(struct Student students[], int *n, char name[]) {
for (int i = 0; i < *n; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i; j < *n - 1; j++) {
students[j] = students[j + 1];
}
(*n)--;
return;
}
}
}
void searchStudent(struct Student students[], int n, char name[]) {
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Found: Name: %s, Age: %d\n",
WAP in C to implement a simple library management system using structures.
#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
char author[50];
int id;
};
void addBook(struct Book books[], int *n, struct Book b) {
books[*n] = b;
(*n)++;
}
void searchBook(struct Book books[], int n, int id) {
for (int i = 0; i < n; i++) {
if (books[i].id == id) {
printf("Found: Title: %s, Author: %s, ID: %d\n", books[i].title, books[i].author, books[i].id);
return;
}
}
printf("Book not found\n");
}
int main() {
struct Book books[100];
int n = 0;
struct Book b;
int choice;
int id;
while(1) {
printf("1. Add Book\n2. Search Book\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter title: ");
scanf("%s", b.title);
printf("Enter author: ");
scanf("%s", b.author);
printf("Enter ID: ");
scanf("%d", &b.id);
addBook(books, &n, b);
break;
case 2:
printf("Enter ID to search: ");
scanf("%d", &id);
searchBook(books, n, id);
break;
case 3:
return 0;
default:
printf("Invalid choice\n");
}
}
}
WAP in C to update the details of a student in an array of structures.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
};
void updateStudent(struct Student students[], int n, char name[], struct Student newDetails) {
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
students[i] = newDetails;
return;
}
}
printf("Student not found\n");
}
int main() {
struct Student students[3] = {{"John", 20}, {"Alice", 19}, {"Bob", 21}};
struct Student newDetails;
char name[50];
printf("Enter name of student to update: ");
scanf("%s", name);
printf("Enter new name: ");
scanf("%s", newDetails.name);
printf("Enter new age: ");
scanf("%d", &newDetails.age);
updateStudent(students, 3, name, newDetails);
for (int i = 0; i < 3; i++) {
printf("Student %d: Name: %s, Age: %d\n", i+1, students[i].name, students[i].age);
}
return 0;
}
WAP in C to store and print the information of N employees using dynamic memory allocation and structures.
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
struct Employee *employees;
int n;
printf("Enter number of employees: ");
scanf("%d", &n);
employees = (struct Employee *)malloc(n * sizeof(struct Employee));
for (int i = 0; i < n; i++) {
printf("Enter name of employee %d: ", i + 1);
scanf("%s", (employees + i)->name);
printf("Enter ID of employee %d: ", i + 1);
scanf("%d", &(employees + i)->id);
printf("Enter salary of employee %d: ", i + 1);
scanf("%f", &(employees + i)->salary);
}
for (int i = 0; i < n; i++) {
printf("Employee %d: Name: %s, ID: %d, Salary: %.2f\n", i + 1, (employees + i)->name, (employees + i)->id, (employees + i)->salary);
}
free(employees);
return 0;
}
WAP in C to read and write employee data to a file using structures.
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
struct Employee e;
FILE *file;
file = fopen("employee.txt", "w");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
printf("Enter name: ");
scanf("%s", e.name);
printf("Enter ID: ");
scanf("%d", &e.id);
printf("Enter salary: ");
scanf("%f", &e.salary);
fprintf(file, "Name: %s\nID: %d\nSalary: %.2f\n", e.name, e.id, e.salary);
fclose(file);
file = fopen("employee.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
fscanf(file, "Name: %s\nID: %d\nSalary: %f\n", e.name, &e.id, &e.salary);
printf("Name: %s\nID: %d\nSalary: %.2f\n", e.name, e.id, e.salary);
fclose(file);
return 0;
}
WAP in C to demonstrate the use of a structure to store data of a linked list and perform basic operations.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* n) {
while (n != NULL) {
printf("%d -> ", n->data);
n = n->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printList(head);
return 0;
}