Computer Programming C: UNIT IV: Structures and Unions

Structures and Unions in C Programming: 2 Marks Definition Answers

Computer Programming C

2 Marks Definition Answers, Multiple Choice Questions, True/False

2 Marks Definition Answers

 

• A Structure contains one or more data items of different data type in which the individual elements can differ in type. A simple structure may contain the integer elements, float elements and character elements etc. The individual structure elements are called members.

 

• Rules for declaring a structure

a) A Structure must end with a semicolon.

b) Usually a structure appears at the top of the source program.

c) Each structure element must be terminated.

d) The structure variable must be accessed with structure variable with abond dot (.) operator.

 

• Difference between Array and Structure

Array

1. An Array is a collection of similar data items.

2. An Array is derived data type.

3. It behaves like a built in data types.

4. An Array can be increased or decreased.

Structure

1. A structure is a collection of dissimilar data items..

2. It is a user defined data type.

3. It must be declared and defined.

4. A structure element can be added if necessary.

 

• Rules for initializing structure

i) The individual data members of structure cannot be initialized.

ii) The structure variables can be initialized at compile time only.

iii) The order of data members in a structure must match the order of values in enclosed brackets.

iv) We can initialize only some of the data members of the structure.

v) The uninitialized data members can be initialised by default with zero (0) for int and float '\0' for character and strings.

 

• 'C' provides a capability that enables the programmer to assign an alternate name to a data type. This is done with a statement known as typedef.

 

• The difference between union and structure is in terms of storage. In structure each member has its own storage location, whereas all the members of union use the same location, although a union may contain many members of different types. When we use union the compiler allocates a piece of storage that is large enough to hold. Like structures, union is also declared by using the keyword union.

 

• Self referential structure: A structure consists of at least a pointer member pointing to the same structure is known as a self‒referential structure. In general it can be expressed as,

 

• A typedef declaration is interpreted in the same way as a variable or function declaration, but the identifier, instead of assuming the type specified by the declaration, becomes a synonym for the type.

 

• A bit field is one bit or a set of adjacent bits with a word. In C, bit fields are used to hold data items. Thus several data items can be packed in a word of memory. The size of bit field varies from 1 to 16 bits in length, the format of a bit field definition is similar to structure definition.

 

• Functions and Similarities Between Union and Structure in C

1. They are both user‒defined data types, and they store different sorts of data together as a single unit.

2. Both of their members can be any type of object. It may include different structures and unions/ arrays. Its members can also contain a bit field.

3. A Union or a Structure can easily pass by value to functions and also return to the value by functions. Every argument must possess the same parameters as that of the function parameter.

4. A Union or Structure passes by the value just like any scalar variable in the form of a corresponding parameter.

5. You can use the "." operator for accessing the members.

 

• Structures allocate memory for all their members, and the total size is the sum of the sizes of all members.

 

• Unions allocate memory only for the largest member. All members share the same memory location.

 

• All members of a structure can store distinct values simultaneously.

 

• Only one member of a union can hold a valid value at any given time. Assigning a value to one member overwrites the value of any other member previously stored in that shared memory..

 

MULTIPLE CHOICE QUESTIONS

 

1. How will you free the allocated memory?

A. remove(var‒name);

B. free(var‒name);

C. delete(var‒name);

D. dalloc(var‒name);

 

2. What is the similarity between a structure, union and enumeration?

A. All of them let you define new values

B. All of them let you define new data types

C. All of them let you define new pointers

D. All of them let you define new structures

 

3. Given the statement, vrb.books. sub=25; which of the following is true?

(A) Structure sub is nested within structure books

(B) Structure books is nested within structure vrb

(C) Structure vrb is nested within structure books

(D) Structure vrb nested within structure sub

 

4. If an integer occupies 4 bytes and a character occupies 1 bytes of memory, each element of the following structure would occupy how many bytes?

struct name

{

int age;

char name [20];

}

(A). 5

(B). 24

(C). 21

(D). 22

 

5. To access a structure element using a pointer, ____ operator is used

(A) dot (.)

(B) pointer (&)

(C) pointer (*)

(D) arrow (‒>)

 

6. Point out the error in the program?

struct emp

{

int ecode;

struct emp *e;

};

A. Error: in structure declaration

B. Linker Error

C. No Error

D. None of above

 

7. What will be the output of the program?

#include <stdio.h>

int main()

{

    union a

   {

        int i;

        char ch[2];

    };

union a u;

u.ch[0]=3;

u.ch[1]=2;

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);

}

A. 3, 2, 515

B. 515, 2, 3

C. 3, 2, 5

D. 515, 515, 4

 

8. What will be the output of the program ?

#include <stdio.h>

int main()

{

struct value

{

    int bit1:1;

    int bit3:4;

     int bit4:4;

}bit={1, 2, 13};

printf("%d,%d, %d\n", bit.bit1, bit.bit3, bit.bit4);

}

A. 1, 2, 13

B. 1, 4, 4

C. ‒1, 2, ‒3

D. ‒1, ‒2, ‒13

 

TRUE/FALSE

 

1. A union cannot be nested in a structure

True/False

2. Nested unions are allowed

True/False

3. Bit fields CANNOT be used in union

True/False

4. One of elements of a structure can be a pointer to the same structure

True/False

5. A structure can be nested inside another structure

True/False

6. The '.' operator can be used access structure elements using a structure variable

True/False

7. Union elements can be of different sizes

True/False

8. A structure can contain similar or dissimilar elements

True/False

9. The '‒>' operator can be used to access structures elements using a pointer to a structure variable only

True/False

10. It is not possible to create an array of pointer to structures

True/False

11. size of union is size of the longest element in the union

True/False

12. The elements of union are always accessed using & operator

True/False

13. A pointer union CANNOT be created

True/False

14. Is there easy way to print enumeration values symbolically?

True/False

15. By default structure variable will be of auto storage class

True/False

16. Is it necessary that the size of all elements in a union should be same?

True/False

17. Can we have an array of bit fields?

True/False

18. Can a structure can point to itself?

True/False

19. All members of a C structure are allocated separate memory locations.

True/False

20. In a C union, all members share the same memory location.

True/False

21. You can access all members of a C union simultaneously and expect them to hold valid, independent values.

True/False

22. The size of a C structure is always the sum of the sizes of its individual members.

True/False

23. The size of a C union is determined by the size of its largest member.

True/False

24. Both structures and unions are user‒defined data types in C.

True/False

25. You can initialize individual members of a structure during its declaration.

True/False

 

Computer Programming C: UNIT IV: Structures and Unions : Tag: Computer Science : Computer Programming C - Structures and Unions in C Programming: 2 Marks Definition Answers


Computer Programming C: UNIT IV: Structures and Unions



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation