How are different types stored in memory

后端 未结 2 910
独厮守ぢ
独厮守ぢ 2020-12-10 15:44

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

2条回答
  •  心在旅途
    2020-12-10 16:20

    1. Yes, ASCII characters are stored by their value. But storing 'A' (65 = 0x41) may be different than storing 65 itself, and how it is done depends on your machine architecture. A char can be stored with a single byte, while a int will be at least 2 bytes (more commonly 4 bytes in modern machines), and so these may be stored differently.

    2. It doesn't. We could have memory that equaled 0x41. The only way this is distinguished between 'A' and 65 is based on how you declared it to the compiler. In other words, if you declared the variable as an int, it will be treated as an int.

    3. There are so few ASCII values that you are able to represent all the possibilities with less than 8 bits. Thus, using 16 bits to represent this would be a waste of memory. In today's systems, this isn't as big of an issue anymore, but on memory limited systems, you might want to use that extra byte for something else instead of wasted space.

    4. More or less, yes. 1 will always be stored as 0000....1, so that the total number of binary digits there equals fills up the space for an int. So on an 8 bit system that will be a 00000000 and a 00000001 in two words, on a 16 bit system that will be 000000000000001 on one word.

提交回复
热议问题