Thread: plz explain the output of the program.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    plz explain the output of the program.

    Code:
    union a
    {
    
     int a;
    char ch[2];
    };
    int main()
    
    { 
    
    union a u;
    
     u.ch[0]=3;
     u.ch[1]=2;
    
    
     printf("%d %d %d", u.ch[0],u.ch[1].u.a);
    }
    and the o/p is


    3 2 515

    some body plz exlain how...

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I think your output will be different each time unless you happen to have

    char - 1 byte
    int - 2 bytes

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed... I get the same result if I make it a short.
    Anyway, it's setting the individual bytes of the int so to speak.
    Byte 1 = 3
    Byte 2 = 2
    On high endian machines, that would make the short equal to 0x0203.
    Input that into a calculator and convert to decimal and you get 515.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GPL license: how does it cover the output of a program?
    By psychopath in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-25-2006, 01:37 AM
  2. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  3. plz can u help me with this program
    By itcs in forum C Programming
    Replies: 6
    Last Post: 07-31-2003, 08:56 AM
  4. Can someone explain this to me plz...
    By CAP in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 01:30 PM
  5. Help Write This Program Plz
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 01:08 AM