Using char

C

Conan1st

Hello everyone!

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...



unsigned short a

a = 321;

printf("a = %d", (char)a);



the display is 'a = 63'...

why is that?

how can I have the right display for a?
 
B

Ben Pfaff

Conan1st said:
I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...



unsigned short a

a = 321;

printf("a = %d", (char)a);

Obviously, 321 doesn't fit in `char' on your platform. Use a
wider type.

(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)
 
M

Mac

Hello everyone!

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...



unsigned short a

a = 321;

printf("a = %d", (char)a);



the display is 'a = 63'...

why is that?

how can I have the right display for a?

printf("a = %hu", a);

When you use a %d with printf, you need to pass an int. %u Is for unsigned
int, and %hu is for a short unsigned int.

If you are determined to use '%d,' then cast 'a' to 'int':

printf("a = %d", (int)a);

Don't forget to add a '\n' at the end of the string, or fflush(stdout),
otherwise you may not ever see the output.

HTH

Mac
--
 
C

Charles Harrison Caudill

Conan1st said:
unsigned short a
a = 321;
printf("a = %d", (char)a);
the display is 'a = 63'...
why is that?

Like someone else said, 321 doesn't fit in 8 bits.
a is a short, on x86 it occupies 16 bits. You're
trying to print after casting it to a char. When
you cast it to a char the c compiler puts in logic
mask out all but the lowest byte ( I actually just
told a lie since x86 is little-endian curse them ).
When you print the result as an int it is
effectively doing this:

unsigned short a = 321;
unsigned char b = 0;
b = ( a & 0x00ff );
printf("%d\n", (int)b);
 
N

nobody

Conan1st said:
Hello everyone!

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...



unsigned short a
Missing semicolon.
a = 321;

printf("a = %d", (char)a);

the display is 'a = 63'...
Are you sure? Unless it is UB (of which I'm not sure), it should output
a = 65
why is that?
because 321 % 256 == 65 (or 321 & 0xff == 65, if you prefer).
how can I have the right display for a?
Mac already answered ...
 
D

Dan Pop

In said:
Obviously, 321 doesn't fit in `char' on your platform. Use a
wider type.

(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)

Huh? What *exactly* is wrong with passing a char to %d, except for the
pathological case when char gets promoted to unsigned int?

Have they dropped the default argument promotions from the C standard
behind my back?

Dan
 
D

Dan Pop

In said:
I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a
a = 321;
printf("a = %d", (char)a);

the display is 'a = 63'...
why is that?

If you can't engage your brain, C programming is not for you.

Isn't it obvious, from your result, that 321 cannot be represented by the
type char on your implementation?

On the vast majority of hosted implementations, char is an 8-bit type.
One of these 8 bits may or may not be used as a sign bit. How many bits
do you need to represent the value 321?
how can I have the right display for a?

By not converting it to a narrower type before displaying it. Isn't it
obvious? What's less obvious is the right printf invocation, to get the
right answer in a portable way:

printf("a = %u\n", (unsigned)a);

The cast to unsigned is needed because, in a portable programming context,
you don't know to what type a is going to be promoted by the default
argument promotions: int or unsigned int. Since no conversion descriptor
can accept either int or unsigned int, you have to bypass the default
argument promotions by converting the value to a type that is no longer
subject to the default argument promotions.

In standard C, this is an issue only with variadic functions, like
printf. For other functions you can always provide a prototype, so that
the default argument promotions are no longer performed by the compiler,
when the function is called.

Dan
 
L

Lew Pitcher

Ben said:
Obviously, 321 doesn't fit in `char' on your platform. Use a
wider type.

(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)

But, Ben, in this case, the char _is_ an int. ;-)

Since printf()'s variadic parameters go through standard function call type
promotion, the char data item is promoted to int as part of the call, before
the call is made.

But you knew that already.

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
 
L

Lew Pitcher

Jirka said:
I think he meant "character", as in printf("%c", a);

Assuming that a is a char or an int, then the above is legal and proper. If
a is a char, it will be promoted to int as part of the call (variadic
functions), and if a is an int, it remains an int. In either case, the
resulting int will be explicitly truncated into a character when printed
using the %c format.

In any case, Ben's comment was directed at the OP's code fragment
printf("a = %d", (char)a);

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
 
J

Jirka Klaue

Lew said:
Assuming that a is a char or an int, then the above is legal and proper.
If a is a char, it will be promoted to int as part of the call (variadic
functions), and if a is an int, it remains an int. In either case, the
resulting int will be explicitly truncated into a character when printed
using the %c format.

What is your understanding of "truncated" in this case?
In any case, Ben's comment was directed at the OP's code fragment
printf("a = %d", (char)a);

Yes, and my comment was directed to you. Did you consider that the
printf("%c", a) is what the OP actually wanted and that Ben wanted
to express exactly that?

Jirka
 
D

Dan Pop

In said:
What is your understanding of "truncated" in this case?


Yes, and my comment was directed to you. Did you consider that the
printf("%c", a) is what the OP actually wanted and that Ben wanted
to express exactly that?

This hypothesis is not worth considering, since the OP was expecting to
see 321 as the output and didn't understand why he got another value.

Dan
 
L

Lew Pitcher

In <[email protected]> Jirka Klaue
What is your understanding of "truncated" in this case?

truncated: past part of "truncate": to shorten by cutting off the top or end

New Websters Dictionary and Thesaurus



--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
 
J

Jirka Klaue

Lew said:
truncated: past part of "truncate": to shorten by cutting off the top or
end

The standard says:

7.19.6.1#8
c If no l length modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written.
...

"Converted" does not necessarily mean "truncated".

Jirka
 
P

pete

Dan said:
Except that it's not clear what is the
*actual* type expected by %hu: is
it int or unsigned int?

I would expect that %hu would *actually* expect
whatever type it is that unsigned short promotes to,
be it int or be it unsigned.
 
D

Dan Pop

In said:
I would expect that %hu would *actually* expect
whatever type it is that unsigned short promotes to,
be it int or be it unsigned.

But the standard doesn't explicitly say so.

Dan
 
P

pete

Dan said:
But the standard doesn't explicitly say so.

I'm starting from the premise that
printf("a = %hu", a);
is defined behavior for the case in question,
and from there I conclude that
something that would cause undefined behavior
(like printf not getting what it's expecting),
isn't happening.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top