Search code examples
casciiasciiencoding

confused about %c and ASCII codes


Why would printf("%c ", 2293552); print 0?

ASCII values are from 0 to 127 I know this must be some cyclic thing but I want a clear explanation. Thank you


Solution

  • The number 2293552 corresponds to 0x22ff30. When printf interprets it as ASCII, it ignores all bits beyond the last eight bits containing 0x30, which is the code for '0'.

    From the C99 standard:

    7.19.1.6.8 -- %c: If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written.