confused about %c and ASCII codes
-
03-07-2021 - |
Domanda
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
Soluzione
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 nol
length modifier is present, the int argument is converted to anunsigned char
, and the resulting character is written.
Altri suggerimenti
Likely %c
is using only the low-order byte of your argument, which is 2293552 & 255 = 48 = '0'
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow