C Programing-Questions On Structures: Exercises, Practice, Solution

In our blog on structures in C programming, we have included a series of questions to help deepen your understanding of this important concept. These questions cover topics such as how to define and access structure members, practical applications of structs, and real-world examples where they can be particularly useful. By working through these questions, you'll gain a solid grasp of how to effectively use structures in your C programs.

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?

  1. 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.
  2. 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.
  3. Easier Data Management: Structures simplify the management of related data, especially when dealing with arrays of structures or passing structured data to functions.
  4. 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 &lt;stdio.h>
#include &lt;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", &amp;n);

    employees = (struct Employee *)malloc(n * sizeof(struct Employee));

    for (int i = 0; i &lt; 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", &amp;(employees + i)->id);
        printf("Enter salary of employee %d: ", i + 1);
        scanf("%f", &amp;(employees + i)->salary);
    }

    for (int i = 0; i &lt; 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;
}

On Key

Related Posts

Practice Question on Java Constructor

Java Constructor Practice Questions with Solutions | Pivot Edu Unit

Java is one of the most popular programming languages, and understanding constructors in Java is essential for mastering Object-Oriented Programming (OOP). If you are preparing for Java interviews, college exams, or simply want to improve your Java skills, this set of Java constructor practice questions will help you. At Pivot Edu Unit, Dehradun, we provide

How to Analyze Data Like a Pro – A Beginner’s Guide to Excel

Introduction In today’s data-driven world, Excel is one of the most powerful tools for analyzing and interpreting data. Whether you’re a student, a professional, or an aspiring data analyst, learning Excel can help you make informed decisions and stand out in your career. In this beginner-friendly guide, we’ll walk you through the key Excel functions

Digital Marketing Interview Question

Top Digital Marketing Interview Questions & Answers for 2025

🚀 Digital Marketing is one of the fastest-growing fields, and companies are actively looking for skilled professionals. Whether you’re a beginner or an experienced marketer, acing a Digital Marketing interview requires a strong understanding of key concepts, tools, and trends. At Pivot Edu Unit, Dehradun, we prepare our students with real-world projects and interview-ready skills.

Data Analysis with SQL & Power BI

Unleash Your Career Potential: Become a Data Analyst with Pivot Edu Unit

In today’s digital era, businesses rely on data-driven decisions to stay competitive. As a result, Data Analysts have emerged as one of the most in-demand professionals globally. If you’re curious about data, have a knack for problem-solving, and want to future-proof your career, becoming a data analyst might be your perfect fit. What Does a