如何被存储在存储器的不同类型 [英] How are different types stored in memory
问题描述
我目前正在写涉及处理字节的C程序。当涉及到字节,我真搞不清楚下面的问题。
-
是由它们的ASCII codeS存储在内存中的字符?说'A'有anscii code 65.因此,它存储在内存中的相同方式整数65?
-
如果是这样,如何机器区分一个字符和一个整数?
-
如果字符由ASCII codeS,一个ascii code是一个整数存储。一个整数,应该至少占2个字节,怎么就一个字只占用1个字节?
-
最后一个是关于不同的体系结构的整数。在16位机,如果1被存储为000 ... 0001,然后在32位计算机上,是1还是存储只是在前面加0以同样的方式?
存储在内存中的字符通过它们的ASCII codeS?说'A'有
anscii code 65.因此,它存储在内存中的相同方式整数65?
块引用>是的,但
字符
在C是一个字节,而一个INT
取决于机器架构。
如果是这样,如何机器区分一个字符和一个整数?
块引用>机code并不关心在内存中再present的字节数。这是编译器的工作到你的code翻译成机器指令做你的程序在做什么。
如果字符由ASCII codeS,存储在ASCII code是一个整数。
一个整数,应该至少占2个字节,怎么只来一个字符
占据1个字节?
块引用>ASCII 可以容纳在一个字节(这是一个
字符大小code>)。非ASCII文本处理较为复杂的C.有
wchar_t的
这是不可移植的,许多人认为这是坏了。 C11 介绍char16_t
和char32_t
,它可以用于UTF-16和UTF-32分别。
最后一个是关于不同的体系结构的整数。上的16位
机,如果1被存储为000 ... 0001,然后在32位机器上,为1
还是存储在前端以同样的方式只是增加0?
块引用>这主要是正确的,但它也取决于建筑的字节顺序。
I am currently writing a C program involving dealing with bytes. When it comes to bytes, I'm really confused about the following questions.
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?
If so, how does the machine distinguish a character and an integer?
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?
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?
解决方案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 anint
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'swchar_t
which is non-portable and many people consider it broken. C11 introduceschar16_t
andchar32_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.
这篇关于如何被存储在存储器的不同类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!