Are characters stored in memory by their ascii codes? Say 'A' has
anscii code 65. So it's stored in memory the same way as integer 65?
Yes, but a char
in C is a single byte, while an int
depends on the machine architecture.
If so, how does the machine distinguish a character and an integer?
Machine code doesn't care what the bytes in the memory represent. It's the job of the compiler to translate your code into machine instructions that do what your program does.
If characters are stored by ascii codes, an ascii code is an integer.
An integer should occupy at least 2 bytes, how come a character only
occupy 1 byte?
ASCII can fit in a single byte (which is the size of a char
). Dealing with non-ASCII text is more complicated in C. There's wchar_t
which is non-portable and many people consider it broken. C11 introduces char16_t
and char32_t
, which can be used for UTF-16 and UTF-32 respectively.
The last one is about integers on different architectures. On a 16-bit
machine, if 1 is stored as 000...0001, then on a 32-bit machine, is 1
still stored the same way just adding 0 at the front?
This is mostly correct, but it also depends on the endianness of the architecture.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…