A Simple question

Geoffrey Rogers grogers at convex.com
Tue Apr 10 03:27:32 AEST 1990


In article <1881 at zipeecs.umich.edu> yhe at 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 at convex.com                 | "Abbie Normal!"                 |
| {sun,uunet,uiucdcs}!convex!grogers |                                 |
+------------------------------------+---------------------------------+



More information about the Comp.lang.c mailing list