What is a union in C?

What is a union in C?

a) A data type that can store different types of data in the same memory location
b) A data type that stores only one type of data
c) A function that returns multiple values
d) A loop that iterates a fixed number of times

Answer:

a) A data type that can store different types of data in the same memory location

Explanation:

A union in C is a data type that allows different types of data to be stored in the same memory location. Unlike structures, where each member has its own memory, all members of a union share the same memory space. This means that at any given time, a union can hold only one of its members’ values. Unions are useful in situations where you need to work with different data types but only one at a time, such as when interpreting a data stream that can contain different types of values.

Understanding unions is important for managing memory efficiently in situations where data types are mutually exclusive and memory usage needs to be minimized.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top