Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C doubt

Reply
Thread Tools

C doubt

 
 
sangeeta chowdhary
Guest
Posts: n/a
 
      07-06-2010
#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
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      07-06-2010
In article <dbbac847-b006-42b4-a53e->,
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;
}
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-06-2010
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) kst- <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"
 
Reply With Quote
 
KSHEELS KSHEELS is offline
Junior Member
Join Date: Sep 2012
Posts: 1
 
      09-06-2012
#include<stdio.h>

int main()
{
int n=5;
printf("n=%*d\n", n, n);
return 0;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
doubt Ajith Nair ASP .Net 6 03-29-2005 01:25 PM
doubt about namespace google manasa sreenivas via .NET 247 ASP .Net 1 05-15-2004 03:05 PM
Doubt to go .Net technology Igbal ASP .Net 1 11-17-2003 06:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57