Thread: Represenation of char type

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    Question Represenation of char type

    Hi,

    I have below piece of code :

    Code:
    #include <stdio.h>
    
    void main()
    {
    union a
    {
    	int i;
    	char ch[2];
    };
    union a key;
    key.i=512;
    printf("\n key.i= %d",key.i);
    printf("\n key.ch[0]= %d",key.ch[0]);
    printf("\n key.ch[1]= %d",key.ch[1]);
    
    key.ch[0]=50;
    printf("\n key.i= %d",key.i);
    printf("\n key.ch[0]= %d",key.ch[0]);
    printf("\n key.ch[1]= %d",key.ch[1]); 
    
    }
    Results:
    -----------
    For the first set when key.i is 512,
    key.ch[0]=0 and key.ch[1]=2 and key.i=512

    However for second set of data ie when ch[0]=50 its showing as
    key.i=562
    key.ch[0]=50 and key.ch[1]=2

    Can anyone please explain me how key.i is 562? This stuff is something to do with endianness.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Because in your first int the binary representation is 1000000000 (leading zeros deleted; I am a lazy git). By setting the low-order bits to 50 (110010) you end up with [11] [00110010] = 562. Or to put it another way for the mathematically challenged, since after the first setting of the int the low-order byte is zero, your math becomes essentially 512 + 50 = 562. Did you expect something different? BTW nothing to do with endian-ness..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    Question

    Hi Jeff,

    I get 0000000000000010 in memory representation when key.i=512

    Now when key.ch[0]=50
    the low order bits will be 0110010
    Since its one byte data it gets overwitten and it will be finally 0000000000110010

    You said "By setting the low-order bits to 50 (110010) you end up with [11] [00110010] = 562."

    Could you please me how did you get [11] [00110010]

    Thank you

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Because you have your bytes out of order sir. Look more closely at my representation of the number 512: 1000000000. Thus in this case the first byte (reading right-to-left) is 00000000 or zero and the second byte (reading the same way) is 10 (leading zeros deleted). Setting byte zero as you have done simple changes or adds the number 50 to your initial value.

    Oh and as for the [11] that was a mistake as I was doing the binary off of the top of my head; it should be [10] [00110010]

    Sorry. What I get for doing binary in my head before coffee.
    Last edited by jeffcobb; 02-08-2010 at 12:20 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Thank you Jeff

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, you should int main, not void main.
    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. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM