Thread: why the result of print statement for a.x isn''t 1?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    32

    why the result of print statement for a.x isn''t 1?

    why the result of print statement for a.x isn''t 1?

    Code:
    #include<stdio.h>
    
    union u{ int x; char y;}a;
    int main ()
    {
        a.x = 1;
        a.y = 'A';  
        printf ("%d ", a.x);
        printf ("%c ", a.y);
        
        printf ("\n %d ", sizeof(a.x));
        printf ("\n %d ", sizeof(a.y));
        return 0;
    }
    result

    65 A
    4
    1

    65 A
    4
    1
    Last edited by skyr6546; 04-03-2020 at 09:57 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    > why the result of print statement for a.x isn''t 1?
    Because you have a union and not a struct.

    With a union, only the last member you wrote is valid to read from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,880
    Because that is how union work on your computer because of byte ordering of the CPU.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    32
    Quote Originally Posted by Salem View Post
    > why the result of print statement for a.x isn''t 1?
    Because you have a union and not a struct.

    With a union, only the last member you wrote is valid to read from.
    why getting wired result

    Code:
    #include<stdio.h>union u
    { 
        int x;
        char y; 
        float z;
        
    }a;
    
    
    int main ()
    {
        a.x = 1;
        a.y = 'A'; 
        a.z = 10.10;
        
        printf ("%d ", a.x);
        printf ("%c ", a.y);
        printf ("%f ", a.z);
        
    
    
        return 0;
    }
    1092721050 � 10.100000

    ...Program finished with exit code 0
    Press ENTER to exit console.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    Did you read the replies you were given? They answer your question.

    If you're not interested in reading what people tell you about unions, then perhaps you shouldn't bother with unions. Change the union to struct and the "weirdness" will vanish.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this statement print a 0?
    By lilywest in forum C Programming
    Replies: 3
    Last Post: 11-22-2013, 10:29 AM
  2. Problem with print out the result
    By pp00 in forum C Programming
    Replies: 11
    Last Post: 11-14-2012, 01:56 AM
  3. Print statement won't print
    By Sakari in forum C Programming
    Replies: 17
    Last Post: 06-20-2012, 06:41 PM
  4. Use result in a new statement
    By SilentPirate007 in forum C Programming
    Replies: 8
    Last Post: 02-16-2010, 08:31 AM
  5. Why i can get the result print to the screen
    By wise_ron in forum C Programming
    Replies: 7
    Last Post: 05-22-2006, 08:06 PM

Tags for this Thread