C doubt

Discussion in 'C Programming' started by sangeeta chowdhary, Jul 6, 2010.

  1. #include<stdio.h>
    #include<string.h>

    int main()
    {
    unsigned char i=0x80;
    printf("%d %d\n",i<<1,sizeof(unsigned char));
    return 0;
    }

    output of this code is 256 1

    binary conversion of i is 10000000
    now if shift it to left by 1,then ishould get zero,as there is only 8
    bits available for i.
    why i am getting 256.
    range of unsigned char is from 0 to +255
     
    sangeeta chowdhary, Jul 6, 2010
    #1
    1. Advertisements

  2. sangeeta chowdhary

    Ike Naar Guest

    In article <>,
    sangeeta chowdhary <> wrote:
    >#include<stdio.h>
    >#include<string.h>
    >
    >int main()
    >{
    > unsigned char i=0x80;
    > printf("%d %d\n",i<<1,sizeof(unsigned char));
    > return 0;
    >}
    >
    >output of this code is 256 1
    >
    >binary conversion of i is 10000000
    >now if shift it to left by 1,then ishould get zero,as there is only 8
    >bits available for i.
    >why i am getting 256.
    >range of unsigned char is from 0 to +255


    Integral promotions.
    The type of expression ``i<<1'' is, most likely, int, not unsigned char.
    Just for fun, change your program as follows:

    #include<stdio.h>
    #include<string.h>

    int main(void)
    {
    unsigned char i=0x80, i1;
    printf("%d %lu\n", i<<1, (long unsigned) sizeof (i<<1));
    i1 = i<<1;
    printf("%d %lu\n", i1, (long unsigned) sizeof i1);
    return 0;
    }
     
    Ike Naar, Jul 6, 2010
    #2
    1. Advertisements

  3. Re: Please use meaningful subject lines [was: C doubt]

    Jonathan Leffler <> writes:
    [...]
    > Sangeeta,
    >
    > If you have questions about C, please ask them with a meaningful subject
    > line, rather than just 'C doubt' each and every time.

    [...]
    > [For instance, this question could have a title such as "Why do I get
    > 256 from left shifting an unsigned char containing 0x80?"]


    Well, that's a bit long; some newsreaders show only the first N
    characters of the subject line. (For mine, with my current settings,
    N seems to be about 43.)

    For this question, something like "left shift of unsigned char"
    would be good. It's not necessary for the entire question to appear
    in the subject (and if it does, it should also be repeated in the
    body of the message). The subject can just be a reminder of what
    the discussion is about. Note that the word "question" (or "doubt")
    can usually be omitted.

    Recommended reading:
    <http://www.catb.org/~esr/faqs/smart-questions.html>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 6, 2010
    #3
  4. sangeeta chowdhary

    KSHEELS

    Joined:
    Sep 6, 2012
    Messages:
    1
    Likes Received:
    0
    i need the explanation for this pgm execution

    #include<stdio.h>

    int main()
    {
    int n=5;
    printf("n=%*d\n", n, n);
    return 0;
    }
     
    KSHEELS, Sep 6, 2012
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Igbal

    Doubt to go .Net technology

    Igbal, Nov 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    663
    Ed Kaim [MSFT]
    Nov 17, 2003
  2. manasa sreenivas via .NET 247

    doubt about namespace google

    manasa sreenivas via .NET 247, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    742
    John Young
    May 15, 2004
  3. Ajith Nair

    doubt

    Ajith Nair, Mar 24, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    778
    nandha
    Mar 29, 2005
  4. Robbe Morris [C# MVP]

    Re: Request / Doubt

    Robbe Morris [C# MVP], May 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    558
    Ajith Nair
    May 4, 2005
  5. Abdul K Shaik

    DDR/SDR-SDRAM Bank Switching Doubt

    Abdul K Shaik, Aug 9, 2003, in forum: VHDL
    Replies:
    1
    Views:
    2,515
    David Jones
    Aug 9, 2003
  6. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    1,012
  7. Replies:
    0
    Views:
    798
  8. Peter Otten
    Replies:
    2
    Views:
    292
    Cousin Stanley
    Aug 10, 2013
Loading...