2077 3

CSC115 ยท 2077 ยท 10 marks

What is structure?How is it different from union?Create a structure named course with name, code and credit_hour as its member.Write a program using this structure to read data of 5 courses and display data of those courses with credit_hour greater than 3.[1+2+2+5]

What is structure?How is it different from union?Create a structure named course with name, code and credit_hour as its member.Write a program using this structure to read data of 5 courses and display data of those courses with credit_hour greater than 3.[1+2+2+5]

C ProgrammingChapter 8: Structure and UnionArray of structure

Quick answer

A structure is a user-defined data type in C that allows grouping of variables of different data types under a single name. Each variable inside a structure is called a member or field.

Structure in C: Definition, Difference from Union, and Program


1. What is a Structure? [1 mark]

Syntax:

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

2. Difference Between Structure and Union [2 marks]

FeatureStructureUnion
Keywordstructunion
Memory AllocationEach member gets its own separate memoryAll members share the same memory
Total SizeSum of sizes of all membersSize of the largest member only
AccessAll members can be accessed and used simultaneouslyOnly one member can hold a value at a time
UsageWhen all members are needed at the same timeWhen only one member is needed at a time (saves memory)

Example:

struct S { int a; float b; };   // size = sizeof(int) + sizeof(float) = 6 bytes
union U  { int a; float b; };   // size = sizeof(float) = 4 bytes (largest member)

3. Structure Named course with Members [2 marks]

struct course {
    char name[50];
    char code[10];
    int credit_hour;
};
  • name stores the course name
  • code stores the course code
  • credit_hour stores the number of credit hours

4. Complete Program: Read 5 Courses and Display Those with credit_hour > 3 [5 marks]

#include<stdio.h>
#include<conio.h>

struct course {
    char name[50];
    char code[10];
    int credit_hour;
};

void main() {
    struct course c[5];
    int i;
    clrscr();

    /* Reading data for 5 courses */
    for(i = 0; i < 5; i++) {
        printf("\nEnter details for Course %d:\n", i + 1);

        printf("Enter Course Name: ");
        scanf("%s", c[i].name);

        printf("Enter Course Code: ");
        scanf("%s", c[i].code);

        printf("Enter Credit Hour: ");
        scanf("%d", &c[i].credit_hour);
    }

    /* Displaying courses with credit_hour greater than 3 */
    printf("\n\nCourses with Credit Hour greater than 3:\n");
    printf("------------------------------------------\n");
    printf("Name\t\tCode\t\tCredit Hour\n");
    printf("------------------------------------------\n");

    for(i = 0; i < 5; i++) {
        if(c[i].credit_hour > 3) {
            printf("%s\t\t%s\t\t%d\n", c[i].name, c[i].code, c[i].credit_hour);
        }
    }

    getch();
}

Sample Output:

Enter details for Course 1:
Enter Course Name: Mathematics
Enter Course Code: MTH101
Enter Credit Hour: 3

Enter details for Course 2:
Enter Course Name: Physics
Enter Course Code: PHY101
Enter Credit Hour: 4

Enter details for Course 3:
Enter Course Name: C_Programming
Enter Course Code: CSC101
Enter Credit Hour: 5
...

Courses with Credit Hour greater than 3:
------------------------------------------
Name            Code            Credit Hour
------------------------------------------
Physics         PHY101          4
C_Programming   CSC101          5

Key Points:

  • struct course c[5] declares an array of structures to hold data of 5 courses.
  • The dot operator (.) is used to access structure members (e.g., c[i].name).
  • A for loop reads data for all 5 courses.
  • A second for loop with an if condition filters and displays only those courses where credit_hour > 3.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.