c program

This is a discussion on c program within the C Programming forums, part of the General Programming Boards category; why does this program output 3,2,515? Code: #include<stdio.h> int main() { union a { short int i; char ch[2]; }; ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    c program

    why does this program output 3,2,515?
    Code:
    #include<stdio.h>
    int main()
    {
        union a
        {
            short int i;
            char ch[2];
        };
        union a u;
        u.ch[0]=3;
        u.ch[1]=2;
        printf("%d %d %d\n",u.ch[0],u.ch[1],u.i);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,456
    because you posted in the C++ forum when this is clearly C code. did you really not see the C forum directly below this one? you should learn the difference between C and C++. it is fairly significant.

    that being said, a short int is (usually) the same size as a char[2], but they represent totally different things in memory. what you really have is a short int sharing memory space with a char[2], and since your array contains 3 and 2, you get (2 * 256) + 3 = 515.
    Last edited by Elkvis; 05-28-2012 at 08:43 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Moved thread
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Try hex output to help with illustration
    Code:
       printf("%x %x %x %d \n", u.ch[0], u.ch[1], u.i, u.i );
    
    /*output:
    3 2 203 515
    
    Your answer lies in how data is stored in bits.
    Here's an illustration of your two bytes with the 3 an 2 in them in binary.
    See how the bit values are combined to determine tha two byte (short) value:
    
            2            3
    0000 0010    0000 0011    <- bits with 1 are "set" or "on"
    
           52    1631 8421  <- each set bit place is worth this much. (read vertically)
           15    2426
           26    8
    
    512  <- add up the bit values.
      2
      1
    ---
    515
    
    */

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    this is clearly C code
    which part of this program is incompatible with C++? seems to compile fine on VC++ and G++ with all warnings enabled.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,610
    Quote Originally Posted by dmh2000 View Post
    which part of this program is incompatible with C++? seems to compile fine on VC++ and G++ with all warnings enabled.
    This part is NOT proper C++. Real C++ programmers use <cstdio> instead; unless using an ancient C++ Compiler.

    Code:
    #include<stdio.h>
    Tim S.
    Note: I am not a real C++ Programmer and I know the difference between C and C++ Headers.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    You could always try
    printf("Answer=%lu\n", (unsigned long)sizeof('a') );

    Within a C program and a C++ program.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21