472,352 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

unsigned char and char print

QQ
Hello,

Here is my simple program
int main()
{
unsigned char a =0x81;
char b = 0x81;
printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b);
printf("cast char to unsigned 0x%x\n",(unsigned char)b);
}
~
And here is the print out:
unsigned char = 0x81(129), char = 0xffffff81(-127)
cast char to unsigned 0x81

The result is what I expected, but I couldn't explain it well.
Could someone give me a help and explain to me what 0xfffff81.
(a char is one byte right?)

And is cast here good? if not, is there any other way I can print out a
char 0x81 as 0x81
printf("cast char to unsigned 0x%x\n",(unsigned char)b);
Thanks a lot!

Apr 6 '06 #1
3 41602
QQ wrote:
Hello,

Here is my simple program
int main()
{
unsigned char a =0x81;
char b = 0x81;
b could be either unsigned or signed. Which is up to the implemntation.
If you want 'b' to be signed, say so. Since UCHAR_MAX may be as low
as 127, and b is signed, you may be assigning an unsigned value to b out
of range. The result of such an assignment is implementation defined.
printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b);
No matter what type b is (unsigned char oe signed char), the above is wrong.
%x is a specifier for unsigned values, %d is a specifier for signed
values. Since a is unsigned, the first %d is wrong.
If b is unsigned, the second %d is wrong. If b is signed, the second
%x is wrong.
printf("cast char to unsigned 0x%x\n",(unsigned char)b);
}
~
And here is the print out:
unsigned char = 0x81(129), char = 0xffffff81(-127)
cast char to unsigned 0x81

The result is what I expected, but I couldn't explain it well.
You have no reasonable expectation as to the output. Your program is
ill-formed.
Could someone give me a help and explain to me what 0xfffff81.
(a char is one byte right?)


It is the int representation of -127. If you use a signed char as an
argument to printf, the usual integer promotions are applied and the
value is passed as a signed int.
Apr 6 '06 #2
QQ schrieb:
Here is my simple program
Unfortunately, it also does not compile.

#include <stdio.h>
int main()
int main (void) {
unsigned char a =0x81;
char b = 0x81;
Note: "char" is a special integer type; usually, the integer type
name alone gives you the same type as if preceded by "signed".
For char, this is not true; char is a type of its own and can be
either signed or unsigned. Your compiler may even offer you to
choose what kind of char you want to have.

If you mean "signed char", write "signed char". Otherwise, you may
run into surprises for another platform, operating system, compiler
or even other compiler settings.
printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b);
Notes: Because printf() is a variadic function, both a and b
are promoted to signed int and passed in this form. This means
that "x" is the wrong conversion specifier. Cast a and b to
unsigned int if you want %u, %x, %o. If you do not mind that
0 will be output as "0" instead of "0x0", you also could use the
# flag (i.e. %#x instead of 0x%x).
printf("cast char to unsigned 0x%x\n",(unsigned char)b);
}
~
And here is the print out:
unsigned char = 0x81(129), char = 0xffffff81(-127)
cast char to unsigned 0x81

The result is what I expected, but I couldn't explain it well.
Could someone give me a help and explain to me what 0xfffff81.
(a char is one byte right?)
Yes. And you are outputting an int (as explained above).
If you want to print out the _representation_ (the actual bits)
of b, then pass
(unsigned int)(*((unsigned char *)&b))
instead of (unsigned char)b or (the correct version)
(unsigned int)((unsigned char)b)
What is the difference?
(unsigned char) b
takes the value of b and if it does not fit into unsigned char,
then (UCHAR_MAX + 1) is added or subtracted as often as necessary
to have b + N*(UCHAR_MAX+1) or b-N*(UCHAR_MAX+1) in the range
that can be represented by unsigned char.
The other one looks at the address where b is stored, pretends that
this is the address of an unsigned char variable and then interprets
the bit pattern found there as unsigned char value.
And is cast here good?


Casts are only necessary in very few places in C. The arguments of
functions with variable argument list (such as printf()) are one
of these places. Whether the cast really gives you what you want
is another question.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 6 '06 #3
Martin Ambuhl schrieb:
QQ wrote:
Hello,

Here is my simple program
int main()
{
unsigned char a =0x81;
char b = 0x81;
b could be either unsigned or signed. Which is up to the implemntation.
If you want 'b' to be signed, say so. Since UCHAR_MAX may be as low
as 127, and b is signed,


ITYM: CHAR_MAX
you may be assigning an unsigned value to b out
of range. The result of such an assignment is implementation defined.
printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b);


No matter what type b is (unsigned char oe signed char), the above is
wrong.
%x is a specifier for unsigned values, %d is a specifier for signed
values. Since a is unsigned, the first %d is wrong.
If b is unsigned, the second %d is wrong. If b is signed, the second
%x is wrong.


I am not sure about this.
If INT_MAX >= UCHAR_MAX, then I'd expect that effectively
(signed int)a and (signed int)b, respectively, is passed.
Could you please clarify how the signedness is transported
via variable argument lists?

<snip>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all...
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the...
17
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is...
3
by: martin paul | last post by:
Sir please consider the following code.I've tried the following code to print whether the MSB of a int/char is zero or one. int main () ...
4
by: Sam | last post by:
Hi, I am using some functions return a base64 encoded string. The functions write it into an unsigned char buffer. I am a little confused as to...
8
by: AGRAJA | last post by:
how to convert unsigned to char? Ref: http://www.thescripts.com/forum/thread477545.html how do I print without the leading ffffff (yet the...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring...
45
by: hank | last post by:
I have this code here, it converts decimal numbers to binary. There is one problem. The output is printed 'in reverse' and I have no clue at all...
21
by: arnuld | last post by:
I have created a program to print the input words on stdout. Input is taken dynamically from stdin. In each word, each input character is allocated...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.