C doubt

K

Keith Thompson

Denis McMahon said:
See other explanations - the issue is that the promoted i from the <<
and the %d are not the data type the OP thinks he is displaying, ie
unsigned char.

So the problem is that the type expected by printf for the "%d" format
(int) doesn't match the type that you think the OP thinks the argument
has.

But in fact the format does match the *actual* type of the argument,
and your attempted explanation was incorrect.

(I do give you credit for pointing out the "%d" vs. size_t mismatch.)
 
K

Keith Thompson

Denis McMahon said:
No they didn't, at least not in the original code,

Incorrect, the types did match in the original code (ignoring the
size_t passed for the second "%d").
or rather, the
original was doing an implicit char to int cast, which meant that what
the OP thought was an operation on an 8 bit char was (probably) actually
taking place on a 32 bit int.

There is no such thing as an "implicit cast". You mean "implicit
conversion" or, more precisely, "promotion". (And it's from unsigned
char to int, not from char to int.)
Consider:

char i = 0x80;
printf("%d",i<<1);

The << operator takes two ints and returns an int, so before the <<
operation takes place, i is implicitly cast to an int.

The << operator takes two operands of integer type, not necessarily
int. The integer promotions are performed on each of the operands,
which is why the char value is implicitly converted to int, *not*
because "<<" requires an int operand (it doesn't). (Note that i
was unsigned char, not char, in the original article.)
Next, the integer value 0x80 is shifted left 1 bit to 0x100;
Correct.

Finally, printf is taking the integer value 0x100 (or binary 0000 0000
0000 0000 0000 0001 0000 0000) and displaying it as a decimal number, 256.

Correct (except that you're assuming int is 32 bits). Note that
"int" and "integer" are not synonymous. There are several integer
types in C; "int" is just one of them. Yes, 0x100 is an integer
value; more precisely, it's an int value.

And there is nothing wrong with the code. There is no type mismatch.
"%d" expects an int argument, and that's exactly what it gets.

(Except perhaps on exotic systems where CHAR_MAX > INT_MAX, which can
happen only if plain char is unsigned, CHAR_BIT >= 16, and some other
unlikely conditions apply.)

The types might not have matched somebody's expectation of what
they should be, but compilers pay no attention to expectations.
 
B

Ben Bacarisse

Denis McMahon said:
See other explanations - the issue is that the promoted i from the <<
and the %d are not the data type the OP thinks he is displaying, ie
unsigned char.

Yes, and I am sure you remember I said that six messages ago in my reply
to yours. In yout original reply you did not explain the problem to the
OP. Instead you asked:

| What data type does %d expect? What does this do to your char?

The answer to these questions are "int" and "nothing", and this
sub-thread was born!
 
N

Nick Keighley

Thank you so much sir,I am just a beginner that's why making these
mistakes.But yes
I will definitely keep you valuable comments to my mind and will try
to improve.

yes but
YOU KEEP MAKING THE SAME MISTAKES

why can't you fix your subject lines?
 

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
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top