CBSE Class 11 » Difference Between » Structure and Union in C

Structure and Union in C

In this article, we will discuss the definition of Structure, definition of Union and difference between structure and union in c on various aspects.

Many built-in data types are available in the C programming language. It also gives users the ability to create their own data kinds. Bit-field, structure, union, typedef, and enumeration are the five methods for constructing bespoke data. We shall learn about structure and union in this blog. In C programming, both structure and union are user-defined data types that may hold any data type. While structure and union appear to have similar responsibilities, there are a few distinctions to be made. Let’s have a look at the distinction between structure and union.

What is Structure?

In the C programming language, a structure is a special data type. Multiple members of various data kinds can be held in a single structure. Structure elements are stored in contiguous memory regions and may be accessed and retrieved at any time. A member or field is a data object in a structure.

The struct statement is used to define a structure. The struct keyword creates a new data type that has several members.

Syntax of Declaring a Structure

struct [structure name]

{

type member_1;

type member_2;

. . .

type member_n;

};

Example of Structure

struct student

 {

int rollno;

char name[57];

string phone;

};

What is Union?

A union is a data type that has been specified by the user. It’s similar to the structure, except that all of its members begin at the same memory position. The union stores a collection of items of diverse data kinds in the same memory region. A user can create a union with numerous members, but only one member can hold a value at any given moment. The total storage space required by the union’s biggest data member is equal to the storage space allotted for the union variable.

Union offers variables that may be accessed in several ways while being in the same memory address. A union is a useful approach to use a single memory region for several tasks.

A union is defined using the union statement. It introduces a new data type that allows numerous member variables of various data types to be stored in the same memory region. The syntax for using the union keyword to define union is similar to that of constructing a structure.

Syntax of Declaring a Union

union [union name]

 {

type member_1;

type member_2;

. . .

type member_n;

};

Example of Union

union Student {

 char name[25];

int age;

string email;

};

Structure and Union: Difference

A structure and a union are both ways of combining multiple types of information about a single item. The fundamental distinction between the two is the manner in which specific data is stored and accessible. The following table summarises the major distinctions between structure and union:

Structure

Union

To define a structure, we utilise the struct statement

To define a union, we use the union keyword

Every member has their own memory place

A memory location is shared by all data members

A change in the value of one data member has no effect on the structure’s other data members

A change in one data member’s value has an impact on the value of other data members

Multiple members can be initialised at the same time

Only the first member can be initialised at a time

Multiple values of various members can be stored in a structure

For all of its members, a union stores one value at a time

The overall size of a structure is the sum of the sizes of all data members

The biggest data member determines the total size of a union

At any moment, users can access or recover any member

Only one member can be accessed or retrieved at a time

Conclusion

We spoke about how structure and unions function and how they vary in this blog. In C programming, both structure and union are user-defined data types that hold numerous members of various data types. Unions help manage memory effectively, whereas structures are utilised when we need to store separate values for all members in the same memory region.

faq

Frequently asked questions

Get answers to the most common queries related to the Programming Examination Preparation.

Which is better to work with, structure or union?

Answer: Unions are easier to debug than structures because they are more adaptable. Structures are ...Read full

What is the primary distinction between a structure and a union?

Answer: A structure’s and a union’s declaration and definition syntaxes are comparable....Read full

What are the drawbacks of using structure in C?

Answer: Memory is allotted to all data members of a C structure. As a result, a structure requires ...Read full

What is the definition of an anonymous union?

Answer: An anonymous union isn’t identified by a tag or a name. It is a member of another org...Read full

What are the drawbacks of using Union in C?

Answer: Drawbacks of using Union are: ...Read full