Complement of unsigned char

O

omisols

Gurus -

Can anyone explain the behavior here?

#include <stdio.h>
#include <stdlib.h>

int main ()
{
unsigned char c = 0;

printf (" c = %u, ~c = %u \n", c, ~c);

return 0;
}

The output for the above chunk of code (when run on a linux box with
gcc) is:
c = 0, ~c = 4294967295

The value for ~c is (2 ^ 32) - 1.

Isn't an unsigned char 8 bits long?
If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

I'm confused.

Thanks,
OmiSols
 
J

Joe Wright

Gurus -

Can anyone explain the behavior here?

#include <stdio.h>
#include <stdlib.h>

int main ()
{
unsigned char c = 0;

printf (" c = %u, ~c = %u \n", c, ~c);

return 0;
}

The output for the above chunk of code (when run on a linux box with
gcc) is:
c = 0, ~c = 4294967295

The value for ~c is (2 ^ 32) - 1.

Isn't an unsigned char 8 bits long?
If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

I'm confused.

Thanks,
OmiSols
You've run afoul of promotions. Look 'em up in your C book. In the
context of the printf function, the expression c yields an int value.
 
S

SM Ryan

# The output for the above chunk of code (when run on a linux box with
# gcc) is:
# c = 0, ~c = 4294967295
#
# The value for ~c is (2 ^ 32) - 1.
#
# Isn't an unsigned char 8 bits long?

Not necessarily.

# If that's the case, should the value of ~c be ( 2 ^ 8 ) - 1 == 255?

The (unsigned char) is converted to (unsigned int) when it shows up
in an expression.


#include <stdio.h>

int main(int N,char **P) {
unsigned char x,y;
x = 0xE1; y = ~x;
printf("%u %u %u %u %u %u\n",x,x|0,y,y|0,~x,~x|0);
return 0;
}


@ cc x.c; a.out
225 225 30 30 4294967070 4294967070
 
K

Keith Thompson

SM Ryan said:
The (unsigned char) is converted to (unsigned int) when it shows up
in an expression.

If int can represent all the values of unsigned char (as it can on
most systems), the unsigned char value is promoted to (signed) int.
 
P

Pedro Graca

I casted it explicitly and it worked as expected.

What did you cast to have it work as expected?
What were the expectations? What was the previous error?

Please quote context in your followups.
Remember to keep attribution lines (the "somebody wrote:" lines) and
trim text not relevant to your followup.
Also read the link in my signature.


If you have a C99 compiler, you can use the "hh" length modifier for
the printf() conversion specification:

#include <stdio.h>
/* #include <stdlib.h> */

int main (void)
{
unsigned char c = 0;

printf("unsigned char: c = %hhu, ~c = %hhu\n", c, ~c);
return 0;
}
 
O

omisols

What did you cast to have it work as expected?
printf (" c = %u, ~c = %u \n", (unsigned char) c, (unsigned char)
(~c)); worked. And by worked I mean, I wanted to see 255 printed and
the above statement did that.
What were the expectations? What was the previous error?
I wanted to see 255 printed, however I was seeing 4294967295 printed
instead.
Please quote context in your followups.
Remember to keep attribution lines (the "somebody wrote:" lines) and
trim text not relevant to your followup.
Also read the link in my signature.
Will do that from now on.
If you have a C99 compiler, you can use the "hh" length modifier for
the printf() conversion specification:

#include <stdio.h>
/* #include <stdlib.h> */

int main (void)
{
unsigned char c = 0;

printf("unsigned char: c = %hhu, ~c = %hhu\n", c, ~c);
return 0;
}
Didn't try this yet. But am sure it'll work.
 
K

Keith Thompson

Will do that from now on.

The article you're replying to was written by Pedro Graca, but I can't
tell that by reading your followup. Please don't delete attribution
lines (e.g., the "(e-mail address removed) writes:" line above).
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top