{"id":2818,"date":"2024-05-10T11:27:36","date_gmt":"2024-05-10T11:27:36","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2818"},"modified":"2024-05-10T11:27:36","modified_gmt":"2024-05-10T11:27:36","slug":"c-unions","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-unions\/","title":{"rendered":"C Unions"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#c-unions\">C Unions<\/a><\/li><li ><a href=\"#syntax-of-union-in-c\">Syntax of Union in C<\/a><\/li><li ><a href=\"#c-union-declaration\">C Union Declaration<\/a><\/li><li ><a href=\"#different-ways-to-define-a-union-variable\">Different Ways to Define a Union Variable<\/a><\/li><li ><a href=\"#1-defining-union-variable-with-declaration\">1. Defining Union Variable with Declaration<\/a><\/li><li ><a href=\"#2-defining-union-variable-after-declaration\">2. Defining Union Variable after Declaration<\/a><\/li><li ><a href=\"#access-union-members\">Access Union Members<\/a><\/li><li ><a href=\"#initialization-of-union-in-c\">Initialization of Union in C<\/a><\/li><li ><a href=\"#example\">Example <\/a><\/li><li ><a href=\"#size-of-union\">Size Of Union<\/a><\/li><li ><a href=\"#difference-between-c-structure-and-c-union\">Difference between C Structure and C Union<\/a><\/li><li ><a href=\"#faq-c-unions\">FAQ- C Unions<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-unions\">C Unions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unions are user-defined data types that, like structures, can contain elements of different data types. However, there is a key distinction between structures and unions. In a structure, each member has its own memory location, and all members can store data simultaneously in memory. This means you can access and manipulate each member independently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a union, all members share the same memory location. As a result, only one member can store data at a given instance. When you assign a value to one member, it will overwrite the data stored in other members of the union.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unions are often used in cases where you want to represent the same memory location in multiple ways, depending on which member of the union is currently active or &#8220;selected.&#8221; This can be useful for memory-efficient representation of data where only one representation is valid at a time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-union-in-c\">Syntax of Union in C<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-union-declaration\">C Union Declaration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you declare a union in C, you are essentially defining the template or structure of the union. This declaration specifies the names of the members, their data types, and the name of the union itself, but it does not allocate memory for the union at that point.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Memory for the union is allocated when you create an instance of the union. In other words, you declare variables of the union type, and it&#8217;s at that point that memory is allocated for the union and its members. This deferred memory allocation is a characteristic shared with other C data types, such as structures.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>union union_name {\n    datatype member1;\n    datatype member2;\n    ...\n};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"different-ways-to-define-a-union-variable\">Different Ways to Define a Union Variable<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We have to define a variable of the union type to start using union members. The two methods which are used to define a union variable.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>With Union Declaration<\/li>\n\n\n\n<li>After Union Declaration<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-defining-union-variable-with-declaration\">1. Defining Union Variable with Declaration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>union union_name {\n    datatype member1;\n    datatype member2;\n    ...\n} var1, var2, ...;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-defining-union-variable-after-declaration\">2. Defining Union Variable after Declaration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>union union_name var1, var2, var3...;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-union-members\">Access Union Members<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The members of union can be accessed using the (.) dot operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1.member1;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where&nbsp;<em>var1<\/em>&nbsp;is referred to as the union variable&nbsp;and&nbsp;<em>member1<\/em>&nbsp;is the&nbsp;member of the union.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above method is mainly used for accessing the members of the union and also works for the nested unions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1.member1.memberA;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>var1<\/em>&nbsp;is referred as  union variable.<\/li>\n\n\n\n<li><em>member1<\/em>&nbsp;is known as a member of the union.<\/li>\n\n\n\n<li><em>memberA<\/em>&nbsp;is a member of member1.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"initialization-of-union-in-c\">Initialization of Union in C<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The initialization of a union is done by initialization its members and, then simply assigning the value to it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var1.member1 = some_value;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate how to use union\n#include &lt;stdio.h&gt;\n \n\/\/ union template or declaration\nunion un {\n    int member1;\n    char member2;\n    float member3;\n};\n \n\/\/ driver code\nint main()\n{\n \n    \/\/ defining a union variable\n    union un var1;\n \n    \/\/ initializing the union member\n    var1.member1 = 15;\n \n    printf(\"The value stored in member1 = %d\",\n           var1.member1);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The value stored in member1 = 15\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"size-of-union\">Size Of Union<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The size of a union always matches the size of its largest member. The smaller members within the union can coexist within the same memory space without any potential for overflow.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to find the size of the union\n#include &lt;stdio.h&gt;\n \n\/\/ declaring multiple unions\nunion test1 {\n    int x;\n    int y;\n} Test1;\n \nunion test2 {\n    int x;\n    char y;\n} Test2;\n \nunion test3 {\n    int arr&#91;10];\n    char y;\n} Test3;\n \n\/\/ driver code\nint main()\n{\n    \/\/ finding size using sizeof() operator\n    int size1 = sizeof(Test1);\n    int size2 = sizeof(Test2);\n    int size3 = sizeof(Test3);\n\n  printf(\"Sizeof test1: %d\\n\", size1);\n    printf(\"Sizeof test2: %d\\n\", size2);\n    printf(\"Sizeof test3: %d\", size3);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sizeof test1: 4\nSizeof test2: 4\nSizeof test3: 40<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-c-structure-and-c-union\">Difference between C Structure and C Union<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Structure<\/th><th>Union<\/th><\/tr><\/thead><tbody><tr><td>The size of the structure is however equal to or greater than the total size of all of its members.<\/td><td>The size of the union is the size of its largest member.<\/td><\/tr><tr><td>The structure contains data from multiple members at the same time.<\/td><td>Only one member will contain data at the same time.<\/td><\/tr><tr><td>Structure is even declared using the struct keyword.<\/td><td>Union is declared using the union keyword.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-unions\">FAQ- C Unions<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697785960334\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are unions in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  A union is a custom data type in C programming. It allows you to group variables of different data types within the same memory location. While you can define a union with multiple members, it&#8217;s important to note that, at any specific instance, only one of its members can store a value. This enables unions to efficiently share memory among various data types.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697785973303\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is a union vs struct in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A union is similar to a struct in that it contains multiple fields (members), and by default, these fields are public. However, the key difference is that, in a union, only one of its fields is used at any given time, and these fields share the same storage space. This flexibility allows a union to store values of different data types at different times, making it a valuable tool for efficient memory usage when you need to represent various data types in a single memory location.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697785996011\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is union in function?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The union of two sets A and B is a new set that contains all the elements present in both sets A and B. Importantly, it ensures that no element is repeated; each element appears in the resulting union set only once. This operation in set theory is commonly denoted by the symbol \u222a and is a fundamental concept when working with sets.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C Unions Unions are user-defined data types that, like structures, can contain elements of different data types. However, there is a key distinction between structures and unions. In a structure, each member has its own memory location, and all members can store data simultaneously in memory. This means you can access and manipulate each member &#8230; <a title=\"C Unions\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-unions\/\" aria-label=\"More on C Unions\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5391,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[489],"class_list":["post-2818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-unions","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2818"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2818\/revisions"}],"predecessor-version":[{"id":10737,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2818\/revisions\/10737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5391"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}