union

This is a discussion on union within the C Programming forums, part of the General Programming Boards category; I am not getting this code. Code: #include <stdio.h> #include <stdlib.h> int main ( void ) { union{ struct{ char ...

  1. #1
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41

    union

    I am not getting this code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
     union{
    	 struct{
    		 char a;
    		 char b;
    		 char c;
    		 char d;
    		} car;
    	 struct{
    		 int i;
    		 int j;
    		}in;
    		long z;
    	}pqr;
    
    	pqr.car.a=3;
    	pqr.car.b=2;
    	pqr.car.c=1;
    	pqr.car.d=0;
    	printf("%d...%d...%ld",pqr.in.i,pqr.in.j,pqr.z);
     return EXIT_SUCCESS;
    }
    output
    ---------
    66051...-370086...66051

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    what specifically don't you get? You have to actually ask a question to get any answers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Print them out using
    %04x for the int, and %08x for the long

    Oh, this code assumes 2-byte ints
    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. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. "generic" union or something
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 08:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 09:12 AM

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