-1

I don't understand the output of this little main :

 int main(int argc, char **argv) {
     char c = '\336';
     unsigned int u = (unsigned int) c;
     printf("%d\n",u); /* I'm waiting for 222  but no ...*/
     exit(EXIT_SUCCESS);
}

Why is this print like my variable is signed int ? How can I to have the value 222 which I want ?

Thank you a lot!

alifirat
  • 2,899
  • 1
  • 17
  • 33

1 Answers1

0

The standard does not specify if char is signed or unsigned by default. So, this is a implementation dependent, and on your machine it seemes it is signed, and '\336' as in this case value -34.

In order to achive what you are expecting, you must:

  1. define c as unisgned char
  2. use %u specifier in printf.

For further explanations, you may take a look here: Is char signed or unsigned by default?

Community
  • 1
  • 1
Paul92
  • 8,827
  • 1
  • 23
  • 37