473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 41873
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 the fundamental integer types? Something like ...
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 question is "why?" "sizeof" and "(char)" have...
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 from when I declare some of my char variables
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 () { unsigned char ch; int...
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 why a base64 encoded string would be using an...
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 result should be char*)?
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 app. Instead of displaying numbers for certain...
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 how to solve this problem. Output of program: ...
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 dynamically. I have ran this program with a file...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.