如何被存储在存储器的不同类型 [英] How are different types stored in memory

查看:113
本文介绍了如何被存储在存储器的不同类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写涉及处理字节的C程序。当涉及到字节,我真搞不清楚下面的问题。


  1. 是由它们的ASCII codeS存储在内存中的字符?说'A'有anscii code 65.因此,它存储在内存中的相同方式整数65?


  2. 如果是这样,如何​​机器区分一个字符和一个整数?


  3. 如果字符由ASCII codeS,一个ascii code是一个整数存储。一个整数,应该至少占2个字节,怎么就一个字只占用1个字节?


  4. 最后一个是关于不同的体系结构的整数。在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 可以容纳在一个字节(这是一个字符)。非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.

  1. 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?

  2. If so, how does the machine distinguish a character and an integer?

  3. 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?

  4. 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 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.

这篇关于如何被存储在存储器的不同类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆