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 8060613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:03:18+00:00 2026-06-05T10:03:18+00:00

my code is as follows:- #include<stdio.h> main() { union a { short int x;

  • 0

my code is as follows:-

#include<stdio.h>

main() {
    union a {
        short int x;
        char y[2];
    };
    union a e;
    e.y[0] = 3;
    e.y[1] = 2;
    printf("%d\n%d\n%d\n", e.y[0], e.y[1], e.x);

    return 0;
}

It gives output as

3
2
515

i didn’t understand that how this 515 comes?

  • 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-05T10:03:21+00:00Added an answer on June 5, 2026 at 10:03 am

    Since I wanted an excuse for ASCII art…

    Declaring the union lets you choose how to interpret its data. In your case, either as an unsigned short or a char [2]. Both of these are 2 bytes long, so your union will refer to a 2-byte section of memory, thusly:

    union a e;
    
        ---------------
    e-> | ???? | ???? |
        ---------------
    

    Now you decide to interpret your union as a character array:

    e.y[0] = 3;
    
        ---------------
    e-> | 0x03 | ???? |
        ---------------
    
    e.y[1] = 2;
    
        ---------------
    e-> | 0x03 | 0x02 |
        ---------------
    

    Then you interpret it as an unsigned short:

    printf("%d\n%d\n%d\n", e.y[0], e.y[1], e.x);
    

    You’re on a little-endian system (as @Oli noted), meaning the least-significant byte is stored first in memory. Which means that when your code looks at an unsigned short, it thinks 0x03 is the least-significant byte.

    So your 2-byte unsigned short is interpreted as 0x0203. And 0x0203 hex is 515 decimal.


    That comment was interesting enough to put in the answer for clarity, I think.

    Let’s say we do this:

    union a {
        int x;
        char y[2];
    };
    
    int main(int argc, char * argv[])
    {
        union a e = {512};
    }
    

    What’s inside? Break it down:

    int is 4 bytes, char [2] is 2 bytes, so the union is 4 bytes long to store the largest datatype. 512 is 0x00000200 in hex. So store that integer little-endian and you have:

        -----------------------------
    e-> | 0x00 | 0x02 | 0x00 | 0x00 |
        -----------------------------
    

    So e.x is 512. e.y[0] is 0 and e.y[1] is 2

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

Sidebar

Related Questions

I have the following code: #include <stdio.h> int main() { printf ("hello world\n"); return
My code is as follows: #include <stdio.h> struct MyData { int id; char msg[255];
My program is as follows; #include <stdio.h> #include <string.h> int main() { char string[]
the code is as follows: #include <stdio.h> main() { int m=123; int n =
I had the following code : #include<stdio.h> void main() { int * a; int
Tried the following code : #include<stdio.h> int main() { int *p,*q; p = (int
First, I wrote a c++ code as follows: #include <cstdio> int main() { int
I have witten a simple c program as follows: #include <stdio.h> #include <regex.h> int
I am getting unusual behaviour with my code, which is as follows #include<stdio.h> struct
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;

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.