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. On 06/07/2010 11:23, 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


    What's the type of "i<<1" ?
    Does changing "<<" to "<<=" change the result shown? If yes, why?
    What's the type of "i<<=1" ? Why is it OK to print it with "%d" ?
    Is <string.h> necessary?
    What's the value of sizeof(unsigned char) according to the C standard?
    What's the type of sizeof(unsigned char) according to the C standard?
    Why is it not OK to print it with %d ?
    What tool can be used to find similar mistakes?
    What is the principle of this tool?
    How much time is needed to create such a tool?

    Francois Grieu
    Francois Grieu, Jul 6, 2010
    #2
    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