0
0
Cprogramming~10 mins

Structure vs union comparison - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a structure named Person.

C
struct Person {
    char name[50];
    int age;
    float height;
};

struct [1] person1;
Drag options to blanks, or click blank then click option'
AStruct
BUnion
CData
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Union' or 'Struct' as a type name instead of the declared structure name.
Forgetting to use the structure name after 'struct'.
2fill in blank
medium

Complete the code to declare a union named Data.

C
union [1] {
    int i;
    float f;
    char str[20];
};

union Data data1;
Drag options to blanks, or click blank then click option'
APerson
BData
CUnion
DStruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Union' or 'Struct' as the union name.
Using the wrong name that does not match the union declaration.
3fill in blank
hard

Fix the error in accessing the union member to assign a value.

C
union Data data1;
data1.[1] = 10;
Drag options to blanks, or click blank then click option'
Ai
Bheight
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access members that belong to a structure but not the union.
Using member names that do not exist in the union.
4fill in blank
hard

Fill both blanks to create a structure and a union variable.

C
struct [1] person2;
union [2] data2;
Drag options to blanks, or click blank then click option'
APerson
BData
CEmployee
DInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up structure and union names.
Using names not declared earlier.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension-like code in C style (simulate with comments) comparing sizes of structure and union.

C
/* Size of struct Person: sizeof(struct [1]) */
/* Size of union Data: sizeof(union [2]) */
/* The size difference is because [3] */
Drag options to blanks, or click blank then click option'
APerson
BData
Cunion shares memory among members
Dstruct shares memory among members
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing structure and union names.
Incorrect explanation about memory sharing.