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. Advertising

  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. Advertising

  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
    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. Advertising

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:
    497
    Ed Kaim [MSFT]
    Nov 17, 2003
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    623
  3. Replies:
    0
    Views:
    557
  4. Peter Otten
    Replies:
    2
    Views:
    110
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    114
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page