473,597 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned char and char print

QQ
Hello,

Here is my simple program
int main()
{
unsigned char a =0x81;
char b = 0x81;
printf("unsigne d char = 0x%x(%d), char = 0x%x(%d)\n",a,a ,b,b);
printf("cast char to unsigned 0x%x\n",(unsign ed 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",(unsign ed char)b);
Thanks a lot!

Apr 6 '06 #1
3 41920
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("unsigne d 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",(unsign ed 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("unsigne d 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",(unsign ed 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)(*((unsigne d 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("unsigne d 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
31498
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 template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
9
3004
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 identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
17
4709
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
2838
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
2100
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 i=0; printf("enter the value\n"); scanf("%d",&ch); if((ch & 10000000)==1) printf("msb is one\n"); else
4
4313
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 unsigned char buffer instead of a signed char buffer? Also if I want to write this to file. Casting the buffer to "char *" & then using fprintf seems to work, but this doesn't seem right to me.
8
4771
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
3256
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 variables I'd like to display a state. So I wanted to use maps for quick look-ups... anyway here's my code for filling and verifying the contents of my map of maps: include<map> #define MAX_LENGTH 80; void gen_enums(void) { char varname =...
45
3771
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: Decimal number: 10 Binary representation: 0101 (It should be 1010, and not 0101...)
21
2900
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 containing a *single* word made of 25525500 letters and this program works fine on it. I will welcome any suggestions for improvement. /* * A program that will ask the user for input and then will print the words on stdout * and will also...
0
7967
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7885
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5428
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2403
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1233
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.