Structure and Union in C | DataTrained

Simran Kumari Avatar

Introduction to Structure and Union in C

Structure and Union in C are basically defined as the sum total of member size and variable.

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

 It can be used to develop software like operating systems, databases, compilers, and so on. It is also defined as a data type available in C that allows combining data items of different kinds. Structures are used to speak for a record whereas union acts for a special data type available in C that allows storing different data types in the same memory location.

Both Structure and Union in C support only assignments = and sizeof operators.

 A Structure and Union in C can be passed by value to functions and returned by value by functions. In structure, we can pass complete sets of records to any function using a single parameter. Union is used when you have to use the same memory location for two or more data members.

What is Structure and Union in C?

What is Structure and Union in C

The structure is the container defined in C to store data variables of different types and also supports the user-defined variable’s storage. On the other hand, Union is also a similar kind of container in C which can also hold the different types of variables along with the user-defined variables. A structure contains an ordered group of data objects. Unlike the elements of an array, the data objects within a structure can have varied data types. Each data object in a structure is a member or field. A union is an object similar to a structure except that all of its members start at the same location in memory.

  • Structure: Structure is an arrangement and organization of interrelated elements in a material object or system, or the object or system so organized.

The structure is a user-defined data type in C language which allows us to combine data of different types together. It helps to construct complex data.

  • Union: A union is a class all of whose data members are mapped to the same address within its object. A union is a special data type available in C that allows to the storage of different data types in the same memory location.

What is the relationship between a Structure Member and Structure in C?

C Structure is a collection of different data types which are grouped together and each element in a C structure is called a member. If you want to access structure members in C, structure variables should be declared. In structure member and structure in C, every member is assigned a unique memory location whereas in Union all the data members share a memory location. 

In structure member change in the value of one data member does not affect other data members in the structure whereas in structure in C change in the value of one data member affects the value of other data members.in structure member, a structure can store multiple values of the different members whereas in structure in C, a union stores one value at a time for all of its members. A structure’s total size is the sum of the size of every data member and in structure in C 

Difference between Structure and Union in C

Differences between Structure and Union in C

Let’s discuss the difference between Structure and Union in C on the basis of keyword, memory, value altering, accessing members, and initialization of members.

Structure 

Union

The keyword union is used to define a union.

The keyword struct is defined as a structure

Union is equal to the size of the largest member.

Greater than or equal to the sum of sizes of its members.

Memory allocated is shared by individual members of the union.

Each member within a structure is assigned a unique storage area of location.

Altering the value of any of the members will alter other members’ values.

Only one member can be accessed at a time

Altering the value of a member will not affect other members of the structure.

Only the first member of the union can be initialized.

Several members of a structure can be initialized at once.

Syntax of declaring Structure

Syntax of declaring Structure in C

Syntax, the arrangement of words in sentences, clauses, and phrases, and the study of the formation of sentences and the relationship of their component parts. 

The declaration of a structure type does not set aside space for a structure. It is only a template for later declarations of structure variables. A previously defined identifier (tag) can be used to refer to a structure type defined elsewhere.

A “structure declaration” names a type and specifies a sequence of variable values (called “members” or “fields” of the structure) that can have different types. An optional identifier, called a “tag,” gives the name of the structure type and can be used in subsequent references to the structure type. A variable of that structure type holds the entire sequence defined by that type. Structures in C are similar to the types known as “records” in other languages.

The declaration of a structure type does not set aside space for a structure. It is only a template for later declarations of structure variables.

Example of Structure in C Programming

  • struct Person { char name[50]; int citNo; float salary; }; Here, a derived type struct Person is defined. Now, you can create variables of this type.

struct employee   /* Defines a structure variable named temp */

{

    char name[20];

    int id;

    long class;

} temp;

The employee structure has three members: name, id, and class. The name member is a 20-element array, and id and class are simple members with int and long types, respectively. The identifier employee is the structure identifier.

Syntax of Declaring Union

Syntax of Declaring Union

The term “syntax” comes from Greek, meaning “arrange together.” The term is also used to mean the study of the syntactic properties of a language.

The syntax for declaring a union is the same as that of declaring a structure except for the keyword struct. Note: The size of the union is the size of its largest field because a sufficient number of bytes must be reserved to store the largest-sized field.

A “union declaration” specifies a set of variable values and, optionally, a tag naming the union. The variable values are called “members” of the union and can have different types. Unions are similar to “variant records” in other languages.

Example of Union in C Programming

Union Employee { int age; char name[50]; float salary; }; Here, a derived type union person is defined.

union sign   /* A definition
and a declaration */

{

    int svar;

    unsigned uvar;

} number;

This example defines a union variable with sign type and declares a variable named number that has two members: svar, a signed integer, and uvar, an unsigned integer. This declaration allows the current value of a number to be stored as either a signed or an unsigned value. The tag associated with this union type is a sign.

Advantages of Structure and Union in C

Advantages of Structure and Union in C

In Structure and Union in C,

  •  Structure Increased productivity,
  •  Structure in C eliminates lots of burdens while dealing with records which contain heterogeneous data items, thereby increasing productivity. Maintainability of code: using structure, we represent complex records by using a single name, which makes code maintainability like a breeze.

 In Structure and Union in C,

  • Union It occupies less memory compared to structure.
  • When you use union, only the last variable can be directly accessed. Union is used when you have to use the same memory location for two or more data members. It enables you to hold data of only one data member.

Disadvantages of Structure and Union in C

Disadvantages of Structure and Union in C

Following are the disadvantages of Structure and Union in C

  • In a union you can use only one union member at a time.
  • All the union variables cannot be initialized or used with varying values at a time.
  • Union assigns one common storage space for all its members.
  • Structure is slower because it requires storage space for all the data.
  • If the complexity of an IT project goes beyond the limit, it becomes hard to manage.
  • Change of one data structure in a code necessitates changes at many other places. Therefore, the changes become hard to track.

What Structure and Union in C Programming

 A Structure and Union in C programming, a key word that creates user defined data type in C/C++. A Structure creates a data type that can be used to group items of possibly different types into a single type. It  is a user-defined datatype in C programming language whereas, Union is a collection of variables of different datatypes in the same memory location. We can define a union with many members, but at a given point of time only one member can contain a value. A structure is a user-defined data type available in C that allows combining data items of different kinds.

Structures are used to represent a record. A union is a special data type available in C that allows storing different data types in the same memory location.

Union is an user defined datatype in C programming language. It is a collection of variables of different datatypes in the same memory location. We can define a union with many members, but at a given point of time only one member can contain a value.

Significance of Structure and Union in C

Significance of Structure and Union in C

Both Structure and Union in C are user-defined data types in C programming that hold multiple members of different data types. Structures are used when we need to store distinct values for all the members in a unique memory location while unions help manage memory efficiently.

C Structures. Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only.

The UNION operator is used to combine the result-set of two or more SELECT statements. Every SELECT statement within UNION must have the same number of columns. The columns must also have similar data types.

Where and why do we use Structure and Union in C

You use a union when your “thing” can be one of many different things but only one at a time. You use a structure when your “thing” should be a group of other things.

A structure is a user-defined data type available in C that allows combining data items of different kinds. In structure and union in C the structures are used to represent a record. A union is a special data type available in C that allows storing different data types in the same memory location.

Structure

  • It is mainly used for storing various data types
  • It occupies space for each and every member written in inner parameters

Union

  • It is mainly used for storing one of the many data types that are available.
  • It occupies space for a member having the highest size written in inner parameters.

Similarities of Structure and Union in C

Similarities of Structure and Union in C

Both Structures and Unions in C support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types four. A structure or a union can be passed by value to functions and returned by value by functions.
Both Structure and Union in C create new data types for using new values and logic of operation.

Both structure and union in C are the custom data types that store different types of data together as a single entity. The members of structure and union in C can be objects of any type, such as other structures, unions, or arrays.

Both structures or unions can be passed by value to a function and also return to the value by functions. The argument will need to have the same type as the function parameter.

To access members, we use the ‘.’ operator.

Conclusion. 

In this blog, we discussed how structure and union in C work and how they differ from each other. Both structure and union in C are user-defined data types in C programming that hold multiple members of different data types. Structures are used when we need to store distinct values for all the members in a unique memory location while unions help manage memory efficiently. 

Structure and Unions in C are both user-defined data types used in c programming. Structure and Union in C share the same concept of storing multiple data types. And all the data types in structure and union in C are accessed with a dot operator.  

Frequently Asked Question’s

1. What is structure and union in C?

 

Structure

Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information. The ,struct keyword is used to define the structure. 

Union

Union is a user-defined datatype in the C programming language. It is a collection of variables of different data types in the same memory location. We can define a union with many members, but at a given point of time, only one member can contain a value.

 

 

2. What is difference between structure and union in C ?

 

Structure

  • The keyword struct is defined as structure
  • Greater than or equal to the sum of sizes of its members.
  • Each member within a structure is assigned a unique storage area of location.
  • Altering the value of a member will not affect other members of the structure.
  • Individual members can be accessed at a time.
  • Several members of a structure can be initialized at once.

Union

  • The keyword union is used to define a union.
  • Union is equal to the size of the largest member.
  • Memory allocated is shared by individual members of the union.
  • Altering the value of any of the members will alter other member values.
  • Only one member can be accessed at a time
  • Only the first member of the union can be initialized

3. What is the advantage of structure in C?

Advantages of structure

Structures gather more than one piece of data about the same subject together in the same place. It is helpful when you want to gather data of similar data types and parameters like first name, last name, etc.

Increased productivity: structure in C eliminates lots of burdens while dealing with records that contain heterogeneous data items, thereby increasing productivity. Maintainability of code: using structure, we represent complex records by using a single name, which makes code maintainability a breeze. The advantages of structure and union in C are mentioned in this blog.

4. What is the syntax for declaring a structure in C?

The general syntax for a struct declaration in C is struct tag_name { type member1; type member2; /* declare as many members as desired, but the entire structure size must be known to the compiler. In Structure and union in C.

5. How do you declare a union in C?

Syntax for declaring a union is same as that of declaring a structure except the keyword struct. Note : Size of the union is the the size of its largest field because sufficient number of bytes must be reserved to store the largest sized field. To access the fields of a union, use dot(.) Get a detailed difference between structure and union in C.

6. What is the relation between structure member and structure?

C Structure is a collection of different data types which are grouped together and each element in a C structure is called a member. If you want to access structure members in C, the structure variable should be declared. Get a detailed difference between structure and union in C.

7. What is the disadvantage of union in C?

Disadvantages of union

You can use only one union member at a time. All the union variables cannot be initialized or used with varying values at a time. Union assigns one common storage space for all its members.

  1. Only one member can be accessed at a particular period of time.
  2. Only one storage space is assigned to all the variables.
  3. All the variables cannot be assigned with varying values at a time.

8. What is union How is it declared?

Union is a user-defined datatype in the C programming language. It is a collection of variables of different data types in the same memory location. We can define a union with many members, but at a given point of time only one member can contain a value.

A “union declaration” specifies a set of variable values and, optionally, a tag naming the union. The variable values are called “members” of the union and can have different types. Unions are similar to “variant records” in other languages. Get a detailed difference between structure and union in C.

9. What is the significance of a structure in C?

C Structures. Structure is a user-defined data type in C language that allows us to combine data of different types together. Structure helps to construct a complex data type that is more meaningful. It is somewhat similar to an Array, but an array holds data of a similar type only. The UNION operator is used to combine the result-set of two or more SELECT statements. Every SELECT statement within UNION must have the same number of columns. The columns must also have similar data types. Get a detailed difference between structure and union in C.

10. What are the advantages of union in C?

Advantages of union

  • It occupies less memory compared to the structure.
  • When you use union, only the last variable can be directly accessed.
  • Union is used when you have to use the same memory location for two or more data members.
  • It enables you to hold data of only one data member.

Tagged in :

More Articles & Posts

UNLOCK THE PATH TO SUCCESS

We will help you achieve your goal. Just fill in your details, and we'll reach out to provide guidance and support.