Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!convex!grogers From: grogers@convex.com (Geoffrey Rogers) Newsgroups: comp.lang.c Subject: Re: A Simple question Message-ID: <101290@convex.convex.com> Date: 9 Apr 90 17:27:32 GMT References: <1881@zipeecs.umich.edu> Sender: news@convex.com Organization: Convex Computer Corporation; Richardson, TX Lines: 26 In article <1881@zipeecs.umich.edu> yhe@eecs.umich.edu (Youda He) writes: +main() +{ + char a=255; + unsigned char b = 255; + printf("a=%X\n",a); + printf("b=%X\n",b); +} + +The result is +a=FFFF +b=FF +on dos, by using zortech and mcs, char is 8 bit long, why a looks like 16bit? +what is the difference of char and unsigned char on printf? + Sign extension. Since 'a' is a char, when it value gets converted to a int (when you call printf), it sign gets extended. With 'b' this does not happen, because it is a unsigned char, so when it gets converted to an int, you just do a bit-wise copy. +------------------------------------+---------------------------------+ | Geoffrey C. Rogers | "Whose brain did you get?" | | grogers@convex.com | "Abbie Normal!" | | {sun,uunet,uiucdcs}!convex!grogers | | +------------------------------------+---------------------------------+