473,320 Members | 1,817 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,320 software developers and data experts.

char change to int, so weird.

Dear all,

I need you help.

here the program:

char a = 0x91;

printf("%x",a);

result: ff ff ff 91

now, i was confused with the result. I think it is 91. but it seems
convert to something.

why?
btw, compiler:gcc machine x86 inter
thanks
Evan
Dec 7 '07 #1
6 2797
"zh*********************@gmail.com"
<zh*********************@gmail.comwrites:
here the program:
Well, no. Just a fragment. A whole program is much better.
char a = 0x91;
printf("%x",a);

result: ff ff ff 91

now, i was confused with the result. I think it is 91. but it seems
convert to something.
Sort of. char is signed on your system. It can probably store numbers
between -128 and 127. 0x91 is 145 which can't be represented in a
(signed) char. At this point you enter implementation defined
territory. On my system, gcc warns me about this implicit overflow
(do you not get such a message?) and sets 'a' to a negative number
(with the bit pattern 0x91).

The 'a' argument to printf gets promoted to an int and printed in
hex. The output you see is the correct output for the negative number
you are printing.

The simplest solution is to declare 'a' as 'unsigned char a;'. 145
will then fit and get printed as you expect.

BTW. I thought this would be a FAQ, but I searched and could not find
it. Maybe someone with more familiarity can find it -- surely it is
in there?

--
Ben.
Dec 7 '07 #2
On 12ÔÂ7ÈÕ, ÉÏÎç11ʱ38·Ö, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"zhangsonglovexiaoniu...@gmail.com"

<zhangsonglovexiaoniu...@gmail.comwrites:
here the program:

Well, no. Just a fragment. A whole program is much better.
char a = 0x91;
printf("%x",a);
result: ff ff ff 91
now, i was confused with the result. I think it is 91. but it seems
convert to something.

Sort of. char is signed on your system. It can probably store numbers
between -128 and 127. 0x91 is 145 which can't be represented in a
(signed) char. At this point you enter implementation defined
territory. On my system, gcc warns me about this implicit overflow
(do you not get such a message?) and sets 'a' to a negative number
(with the bit pattern 0x91).

The 'a' argument to printf gets promoted to an int and printed in
hex. The output you see is the correct output for the negative number
you are printing.

The simplest solution is to declare 'a' as 'unsigned char a;'. 145
will then fit and get printed as you expect.

BTW. I thought this would be a FAQ, but I searched and could not find
it. Maybe someone with more familiarity can find it -- surely it is
in there?

--
Ben.
i understand what you said , but another question:
if it is overflow why the value should be bit pattern?
is it a rule or something else?
Dec 7 '07 #3
On Fri, 07 Dec 2007 03:38:22 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote in comp.lang.c:
"zh*********************@gmail.com"
<zh*********************@gmail.comwrites:
here the program:

Well, no. Just a fragment. A whole program is much better.
char a = 0x91;
printf("%x",a);

result: ff ff ff 91

now, i was confused with the result. I think it is 91. but it seems
convert to something.

Sort of. char is signed on your system. It can probably store numbers
between -128 and 127. 0x91 is 145 which can't be represented in a
(signed) char. At this point you enter implementation defined
territory. On my system, gcc warns me about this implicit overflow
(do you not get such a message?) and sets 'a' to a negative number
(with the bit pattern 0x91).

The 'a' argument to printf gets promoted to an int and printed in
hex. The output you see is the correct output for the negative number
you are printing.
Correction, there is no "correct output" for his printf() statement,
since it produces undefined behavior. He is using the "%x" conversion
specifier, which requires an unsigned int parameter. But he is
passing a signed int with a negative value.

Argument compatibility in *printf() between signed and unsigned types
is guaranteed only when the value is within the range of both types,
and that is never true when passing a signed type with a negative
value to match an unsigned type conversion specifier.

It would be the correct output for:

printf("%x", (unsigned int)a);
The simplest solution is to declare 'a' as 'unsigned char a;'. 145
will then fit and get printed as you expect.

BTW. I thought this would be a FAQ, but I searched and could not find
it. Maybe someone with more familiarity can find it -- surely it is
in there?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Dec 7 '07 #4
zh*********************@gmail.com wrote:
Dear all,

I need you help.

here the program:

char a = 0x91;

printf("%x",a);
Well, no. That isn't anywhere close to being a program.
result: ff ff ff 91
Well, no. I don't believe those three embedded spaces for a minute.
>
now, i was confused with the result. I think it is 91. but it seems
convert to something.

why?
This results from (a) 'char' being signed on your implementation and
(b) sign extension when 0x91 has the same bit pattern as a negative
char, which then is promoted to a negative int when passed to printf.

The program below, which incorporates several things which should never
appear in well-written code, should illustrate the problem if you
examine the program and the output. Note
(1) This is a horrid program and should not be a model for anything you
will ever write, and
(2) The output reported below the program listing is for one
implementation. There is no way for me to predict what your
implementation will actually do with it.

#include <stdio.h>

int main(void)
{
/* note that 'char' may be either unsigned or signed */
unsigned char a = 0x91;
signed char b = 0x91; /* possible overflow */
unsigned int c;
signed int d;

printf
("note that each attempt to print an unsigned value using
\"%%x\"\n"
"is dodgy and should not, in fact, be done.\n\n");

printf("unsigned char a as (unsigned) hex = %x\n", a);
printf("signed char b as (unsigned) hex = %x\n\n", b);
c = a;
d = a;
printf
("unsigned int c and signed int d assigned (unsigned char) 0x91\n"
"unsigned int c as (unsigned) hex = %x\n"
"signed int d as (unsigned) hex = %x\n\n", c, d);
c = b;
d = b;

printf
("unsigned int c and signed int d assigned (signed char) 0x91\n"
"unsigned int c as (unsigned) hex = %x\n"
"signed int d as (unsigned) hex = %x\n\n", c, d);
return 0;
}

[output on one implementation]
note that each attempt to print an unsigned value using "%x"
is dodgy and should not, in fact, be done.

unsigned char a as (unsigned) hex = 91
signed char b as (unsigned) hex = ffffff91

unsigned int c and signed int d assigned (unsigned char) 0x91
unsigned int c as (unsigned) hex = 91
signed int d as (unsigned) hex = 91

unsigned int c and signed int d assigned (signed char) 0x91
unsigned int c as (unsigned) hex = ffffff91
signed int d as (unsigned) hex = ffffff91
Dec 7 '07 #5
Jack Klein <ja*******@spamcop.netwrites:
On Fri, 07 Dec 2007 03:38:22 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote in comp.lang.c:
<snip>
>The 'a' argument to printf gets promoted to an int and printed in
hex. The output you see is the correct output for the negative number
you are printing.

Correction, there is no "correct output" for his printf() statement,
since it produces undefined behavior. He is using the "%x" conversion
specifier, which requires an unsigned int parameter. But he is
passing a signed int with a negative value.
Yes, thanks. I forget %x is for unsigned.
Argument compatibility in *printf() between signed and unsigned types
is guaranteed only when the value is within the range of both types,
Where does this special leeway come from? In paragraph 9 of the
fprintf section its says:

"If any argument is not the correct type for the corresponding
conversion specification, the behavior is undefined."

--
Ben.
Dec 7 '07 #6
On 7 Dec, 07:17, Martin Ambuhl <mamb...@earthlink.netwrote:

<snip>
printf
("note that each attempt to print an unsigned value using
\"%%x\"\n"
"is dodgy and should not, in fact, be done.\n\n");
ITYM signed, or possibly negative signed value ?

Dec 7 '07 #7

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

Similar topics

2
by: stevenkblack | last post by:
I have a question about moving to UTF-8. We will have to include Double byte characters in our database so I created a new database that was UTF-8. I moved the table sturcture over and am using...
1
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass =...
8
by: scrodchunk | last post by:
I'm having a weird problem with printing bytes in hex format. I have a bunch of print statements that are working okay, then using the identical formatting later in my code I get some thing where...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
7
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
7
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created...
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.