Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8016727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:37:19+00:00 2026-06-04T20:37:19+00:00

I could not understand how Union works.. #include <stdio.h> #include <stdlib.h> int main() {

  • 0

I could not understand how Union works..

#include <stdio.h>
#include <stdlib.h>

int main()
{
    union {
    int a:4;
    char b[4];
    }abc;
abc.a = 0xF;

    printf(" %d, %d, %d, %d, %d, %d\n", sizeof(abc), abc.a, abc.b[0], abc.b[1], abc.b[2], abc.b[3]);

    return 0;
}

enter image description here
In the above program.
I made int a : 4;
So, a should taking 4 bits.
now I am storing, a = 0xF; //i.e a= 1111(Binary form)
So when I am accessing b[0 0r 1 or 2 or 3] why the outputs are not coming like 1, 1, 1, 1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:37:20+00:00Added an answer on June 4, 2026 at 8:37 pm

    Your union’s total size will be at least 4 * sizeof(char).

    Assuming the compiler you are using handles this as defined behavior, consider the following:

    1. abc is never fully initialized, so it contains a random assortment of zeros and ones. Big problem. So, do this first:
      memset(&abc, 0, sizeof(abc));
    2. The union should be the size of its largest member, so you should now have 4 zeroed-out bytes: 00000000 00000000 00000000 00000000
    3. You are only setting 4 bits high, so your union will become something like this:
      00000000 00000000 00000000 00001111 or
      11110000 00000000 00000000 00000000.

      I’m not sure how your compiler handles this type of alignment, so this is the best I can do.

    You might also consider doing a char-to-bits conversion so you can manually inspect the value of each and every bit in binary format:
    Access individual bits in a char c++

    Best of luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> int main () { printf(%#04x,50); } Some one showed me above code and
I could not understand BorderThickness={TemplateBinding BorderThickness} . Here the code: <ControlTemplate TargetType={x:Type wpftoolkit:DataGridCell}> <Border
I could not understand this error, the scenario is: This is the directory structure:
I was studying the radix sort algorithm but I could not understand some of
I have come across the following jQuery code but could not understand it. What
I could not understand what this javascript line does. chkBox.setAttribute(CheckList, range); What is the
I'm required to implement a license management and i could not understand the difference
I've read and searched about MongoDB's JSON-BSON constructions but I do not understand (could
When I was trying to learn from an existing program, I could not understand
I do not understand how following code works. Specifically, I do not understand using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.