menu search
brightness_auto
Ask or Answer anything Anonymously! No sign-up is needed!
more_vert
This question is related to programming language C...

8 Answers

more_vert

Structure Programming Language:

  1. The keyword struct is used to declare a structure
  2. structure is a user define datatype
  3. individual member can be accessed at a time.
  4. structure elements don't share their memory.
  5. all the member in a structure are active a a time.
  6. memory is allocated for each member


Union in C Programming Language:

  1. The keyword union used to declare a union
  2. union is also user defined datatype
  3. only one member can be access at a time.
  4. largest elements memory is shared by other elements of union
  5. only one data member is active at a time.
  6. memory is allocated for largest member.
thumb_up_off_alt 1 like thumb_down_off_alt 0 dislike
more_vert
A structure is basically a user defined data which is available in C language.

Structure enable us to combine different data items.

A union is special data type available in C language.

Union allows storing the data types in the memory.
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
Struct tag {

Member 1;

Member2;

Member n;

};

Union contain members whose individual data types may differ from one another.

Union tag {

Member 1;

Member2;

Member m;

};
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
```C // Structure struct student { int id; char name[20]; }; // Union union student { int id; char name[20]; }; // Difference The main difference between a structure and a Union is that a structure is a collection of variables of different data types while a union is a collection of variables of the same data type. A structure allows you to refer to its members by their names whereas a union, since all its members share the same memory location, all of them must be
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
In C, "struct" is used to define a collection of variables of different types, while "union" is used to define variables that share the same memory space.
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
In C programming language, structure and union are both composite data types that allow you to group related data elements of different data types together. However, there are some key differences between the two:

Memory allocation:

A structure allocates separate memory for each member variable declared inside the structure.

A union allocates memory that is shared by all its member variables, and only enough memory to hold the largest member.

Size:

The size of a structure is the sum of the sizes of all its members.

The size of a union is the size of its largest member.

Accessing members:

Members of a structure are accessed using dot (.) notation.

Members of a union are accessed using the same syntax as structure members, but only one member can be accessed at a time.

Initialization:

You can initialize individual members of a structure.

You can initialize only the first member of a union.

Here's an example that demonstrates the difference between a structure and a union

#include <stdio.h>

struct student {

    int roll_no;

    char name[20];

    float marks;

};

union data {

    int i;

    float f;

    char c;

};

int main() {

    // Declare and initialize a structure variable

    struct student s = { 1, "John", 85.5 };

    printf("Size of structure: %ld\n", sizeof(s)); // Output: 28

    // Declare and initialize a union variable

    union data d = { 'A' };

    printf("Size of union: %ld\n", sizeof(d)); // Output: 4

    return 0;

}    

In the example above, the student structure contains three members of different data types, so its size is the sum of the sizes of those members, which is 28 bytes on a typical 64-bit system. On the other hand, the data union contains three members of different data types that share the same memory, and its size is only 4 bytes, because char is the smallest member.

In summary, structures and unions are both powerful tools for organizing data in C programming language, and their different features make them useful in different situations.
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
In the case of a Structure, there is a specific memory location for every input data member. Thus, it can store multiple values of the various members. In the case of a Union, there is an allocation of only one shared memory for all the input data members. Thus, it stores one value at a time for all of its members.
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
The main difference between a structure and a Union is that a structure is a collection of variables of different data types while a union is a collection of variables of the same data type. A structure allows you to refer to its members by their names whereas a union, since all its members share the same memory location, all of them must be
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
Whenever you have a question in your mind, just drop it on Answeree. Help our community grow.
...