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

This answer is restricted. Please login to view the answer of this question.

Login Now

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name that is used to select it from the structure. The variables are called members of the structure. A structure is a convenient way of grouping general pieces of related information together.

The difference between structure and union is:

Structure Union
1) Each member within a structure is assigned its own unique storage. It takes more memory than a union. 1) All members within the union share the same storage area of computer memory. It takes less memory than structure.
2) The amount of memory required to store a structure is the sum of the size of all members. 2) The amount of memory required to store a union is the same as a member that occupies the largest memory.
3) All the structures can be accessed at any point in time. 3) Only one member of the union can be accessed at any given time.

Source: https://codehelppro.com/detail/c-programming/structure/

Program:

#include <stdio.h>
#include <string.h>

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

int main(){

    int i;
    struct student st[5];

    printf("Enter Records of 5 courses:\n");
    for (i = 0; i < 5; i++){
        printf("\nEnter Subject Name: ");
        scanf("%s", &st[i].name);

        printf("Enter Subject Code: ");
        scanf("%s", &st[i].code);

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

        printf("\n");
    }

    printf("\n\nCourse Information List:\n");
    for (i = 0; i < 5; i++){

        if( st[i].credit_hour > 3 ){
            printf("\nSubject Name:%s, Subject Code:%s, Subject Credit Hour:%d\n", st[i].name, st[i].code, st[i].credit_hour);
        }
    }

    return 0;
}

The output of the above program is

Enter Records of 5 courses:

Enter Subject Name: Physic
Enter Subject Code: PHY
Enter Subject Code: 3


Enter Subject Name: Math
Enter Subject Code: MTH
Enter Subject Code: 4


Enter Subject Name: DigitalLogic
Enter Subject Code: DL
Enter Subject Code: 4


Enter Subject Name: C
Enter Subject Code: c
Enter Subject Code: 3


Enter Subject Name: IIT
Enter Subject Code: IIT
Enter Subject Code: 4



Course Information List:

Subject Name:Math, Subject Code:MTH, Subject Credit Hour:4
Subject Name:DigitalLogDL, Subject Code:DL, Subject Credit Hour:4
Subject Name:IIT, Subject Code:IIT, Subject Credit Hour:4
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .