This blog is under construction

Saturday, 11 January 2014

How to initialize union variables in C

Let us see all possible ways to initialize a union variable.  Please remember that we can access one data member at a time in the case of union.

Method 1:
union value {
     int x;
     float y;
};
union value obj = {5};

Here, initialization is done after union definition.  5 is assigned to the member x of union variable obj.

Method 2:
union value {
     int x;
     float y;
}obj = {5};

Here, initialization is done while defining the union.  5 is assigned to the member x of union variable obj.

Method 3:
union value {
     int x;
     float y;
};
union value obj = {.y = 5.0};

Here, initialization is done after union definition.  5.0 is assigned to the member y of union variable obj.  And we have used dot(.) operator for initialization.

Method 4:
union value {
     int x;
     float y;
};
union value obj = {y:5.0};

This method is similar to method 3.  Here, color(:) is used instead of '=' and period before union member is removed


Example C program to illustrate union variable initialization:

  #include <stdio.h>
  union value {
        int x;
        float y;
  }obj1 = {5};  //initializing union variable

  int main() {
        /* declaration and initialization for union variables */
        union value obj2 = {6};
        union value obj3 = {.y = 7.1};
        union value obj4 = {y:8.2};

        /* printing the values */
        printf("obj1.x : %d\n", obj1.x);
        printf("obj2.x : %d\n", obj2.x);
        printf("obj3.y : %f\n", obj3.y);
        printf("obj4.y : %f\n", obj4.y);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  obj1.x : 5
  obj2.x : 6
  obj3.y : 7.100000
  obj4.y : 8.200000



14 comments:

  1. C tutorial with free and simple example on Data Types & Variables, Conditionals, Loops, Arrays & Strings etc. Here you can download simple and easy online c tutorial for beginners .C Programming online test.

    ReplyDelete
    Replies
    1. Hello Everyone !

      USA SSN Leads/Dead Fullz available, along with Driving License/ID Number with good connectivity.

      All SSN's are Tested & Verified.

      **DETAILS IN LEADS/FULLZ**

      ->FULL NAME
      ->SSN
      ->DATE OF BIRTH
      ->DRIVING LICENSE NUMBER
      ->ADDRESS WITH ZIP
      ->PHONE NUMBER, EMAIL
      ->EMPLOYEE DETAILS

      *Price for SSN lead $2
      *You can ask for sample before any deal
      *If you buy in bulk, will give you discount
      *Sampling is just for serious buyers

      ->Hope for the long term business
      ->You can buy for your specific states too

      **Contact 24/7**

      Whatsapp > +923172721122

      Email > leads.sellers1212@gmail.com

      Telegram > @leadsupplier

      ICQ > 752822040

      Delete
  2. Good info on this post. You have shared very valuable info here.
    c programming training

    ReplyDelete
  3. To know the programming technique: "Make Your Program Trial Version in C" visit:
    udebayan.blogspot.com

    ReplyDelete
  4. To know the programming technique: "Make Your Program Trial Version in C" visit:
    udebayan.blogspot.com

    ReplyDelete
  5. Nice article admin thanks for share your atricle keep share your knowledge i am waiting for your new post check mens winter jackets polo shirts kindly review and reply me

    ReplyDelete
  6. Hello Everyone !

    USA SSN Leads/Dead Fullz available, along with Driving License/ID Number with good connectivity.

    All SSN's are Tested & Verified.

    **DETAILS IN LEADS/FULLZ**

    ->FULL NAME
    ->SSN
    ->DATE OF BIRTH
    ->DRIVING LICENSE NUMBER
    ->ADDRESS WITH ZIP
    ->PHONE NUMBER, EMAIL
    ->EMPLOYEE DETAILS

    *Price for SSN lead $2
    *You can ask for sample before any deal
    *If you buy in bulk, will give you discount
    *Sampling is just for serious buyers

    ->Hope for the long term business
    ->You can buy for your specific states too

    **Contact 24/7**

    Whatsapp > +923172721122

    Email > leads.sellers1212@gmail.com

    Telegram > @leadsupplier

    ICQ > 752822040

    ReplyDelete
  7. **SELLING SSN+DOB FULLZ**

    CONTACT 24/7
    Telegram > @leadsupplier
    ICQ > 752822040
    Email > leads.sellers1212@gmail.com

    >>1$ each without DL/ID number
    >>2$ each with DL
    >>5$ each for premium (also included relative info)

    Price will be reduce in Bulk order

    FORMAT OF LEADS/FULLZ/PROS

    ->FULL NAME
    ->SSN
    ->DATE OF BIRTH
    ->DRIVING LICENSE NUMBER WITH EXPIRY DATE
    ->COMPLETE ADDRESS
    ->PHONE NUMBER, EMAIL, I.P ADDRESS
    ->EMPLOYMENT DETAILS
    ->REALTIONSHIP DETAILS
    ->MORTGAGE INFO
    ->BANK ACCOUNT DETAILS

    >All Leads are Spammed & Verified.
    >Fresh spammed data of USA Credit Bureau
    >Good credit Scores, 700 minimum scores
    >Invalid info found, will be replaced.
    >Payment mode BTC, ETH, LTC, PayPal, USDT & PERFECT MONEY

    ''OTHER GADGETS PROVIDING''

    >SSN+DOB Fullz
    >CC with CVV
    >Photo ID's
    >Dead Fullz
    >Carding Tutorials
    >Hacking Tutorials
    >SMTP Linux Root
    >DUMPS with pins track 1 and 2
    >Sock Tools
    >Server I.P's
    >HQ Emails with passwords

    Contact 24/7

    Email > leads.sellers1212@gmail.com
    Telegram > @leadsupplier
    ICQ > 752822040

    ReplyDelete
  8. Watch Bade Achhe Lagte Hain Season 2 All New Episodes Online in High Quality Videos, Bade Achhe Lagte Hain 2 Latest Complete Episodes and Free Download.

    badeachhelagtehain2.com

    ReplyDelete

  9. Excellent article. I am experiencing some of these issues as well..

    ReplyDelete

  10. Excellent article. I am experiencing some of these issues as well..https://aniworld.mom

    ReplyDelete