简体   繁体   中英

comparison of characters in C & python

I am trying to implement a C code in python:

int main()
{
  char ch=291;
  printf("%d %c",ch,ch);
  return 0;
}

In python:

ch=291
print "%d %c" % (ch,ch)

But it's giving an error...

In C ,character value after 255 again returns to 0, Is that type of concept is not applicable for python?

it's actually giving:

OverflowError: unsigned byte integer is greater than maximum

and it's because the %c format expects a single byte, which is 0x11111111 (255) maximum. And 291 is 0b100100011 which is 9 bits.

In c I'm not sure why it shows 0 , but it's definitely a way to tell you there's an error, overflowing the byte in the format.

>>> ch=291   
>>> print "%d %s" % (ch,ch)
    291 291

Here ch is variable where assign as 291 value. Use %s for string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM