Thread: how come this output?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    how come this output?

    [code]

    Dear All,

    can anybody help me to following doubts i am thankful to you

    1.
    Code:
     #include<stdio.h>
     void main()
     {
        int a=3;
        printf("%d", ~a);
     }
    
     It showing -4 how come?
    2.
    Code:
    #include<stdio.h>
     void main()
    {
       union
     {
        unsigned i1:8;
        unsigned i2:8;
        unsigned i3:8;
        unsigned i4:8;
    }uu;
     
    
    uu. i1=257;
    printf("\n %d::%d" ,sizeof(uu), uu.i2);
    }
    
    it showing 4 ,1 answer how come?
    also if i print any of uu.i1, uu.i2, uu.i3 all showing 1 only please help me?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What value do you EXPECT for ~3? Do you understand what ~3 means?

    Do you understand how unions work? Do you understand how bitfields work? In your case, you have a bitfield that is 8 bits wide. What does that mean for a value of 257?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Quote Originally Posted by matsp View Post
    What value do you EXPECT for ~3? Do you understand what ~3 means?

    Do you understand how unions work? Do you understand how bitfields work? In your case, you have a bitfield that is 8 bits wide. What does that mean for a value of 257?

    --
    Mats
    hai Mats..
    thanks for your reply.... ~3 means 2's complement of 3 right? Can u explain me that union example so that i wil understand...i am new to programming thats y i am asking you silly questions

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It starts to fall apart with the void main (main returns int).

    > Can u explain me that union example so that i wil understand
    It's the same reason that (for 8-bit chars)
    unsigned char c = 257;
    performs modulo 256 on the value to make it fit in the space available.
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Shidlingayya View Post
    hai Mats..
    thanks for your reply.... ~3 means 2's complement of 3 right? Can u explain me that union example so that i wil understand...i am new to programming thats y i am asking you silly questions
    Yes, but what does 2's complement actually achieve - or put another way, what do you find strange or don't understand about the result -4.

    As to unions, perhaps you would look that up in your C book?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM