计算机如何区分类型?

huangapple go评论76阅读模式
英文:

How does the computer make the difference between types?

问题

我想知道我的计算机如何区分完全相同的0和1的集合。我不太明白应该问什么,但我想知道,例如,当我在C中有以下代码时:

int main(){
   __uint8_t a = 97;
   printf("这是一个字符:%c\n这是一个数字:%d\n", a, a);
   return 0;
}

它是如何知道何时应该打印'a'或97的。我知道这与%c和%d有关,但实际上发生了什么?

谢谢大家!

英文:

I am wondering how my computer can make the difference between the exact same set of 0s and 1s. I don't really know what should be the question but I want to know for instance when I have in C :

int main(){
   __uint8_t a = 97;
   printf(" Here is a char : %c\n Here is a number : %d\n", a, a);
   return 0;
}

How does it know when he rather should print 'a' or 97. I know it is due to the %c and %d, but what is going on really ?

Thank you all !

答案1

得分: 1

%c%d 是你提到的转换说明符,它们每一个都有特定的含义。它们预先确定了对应参数的处理方式、格式化和打印方式。

引用自 C11,第 §7.21.6.1 章,一些示例:

  1. 转换说明符及其含义如下:

d,i int 参数会转换为带有符号的十进制样式 [−]dddd.

c 如果没有 l 长度修饰符,那么 int 参数会转换为一个 unsigned char,并将结果字符写入。

就像这样,每个转换说明符都有相关的规则,用于解释和打印提供的参数或忽略它们(例如:%%)。

英文:

That %c or %d you mentioned are called conversion specifier, each one of them have specific meaning. They are pre-decided that how the corresponding argument will be treated, formatted and printed.

Quoting from C11, chapter §7.21.6.1, some examples:

> 8. The conversion specifiers and their meanings are:
>
> > d,i The int argument is converted to signed decimal in the style [−]dddd.
>
> and
>
> > c If no l length modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written.

Like that, each conversion specifier has associated rules on how they interpret and print the supplied arguments or ignore them (ex: %%).

答案2

得分: 1

当你使用%c来打印时,计算机会查找ASCII表。计算机只能理解数字(二进制),而这个表是如何将数字转换为字符的参考(见下文),如你所见,a = 97在十进制中等于0110 0001在二进制中。在代码中,你可以要求以不同的格式显示一个值(%d表示十进制,%f表示浮点数,%c表示字符...),但在计算机中始终是一个二进制字。

计算机如何区分类型?

英文:

When you ask to print with %c the computer will look toward the ASCII table. Computer can only understand numbers (binary) and this table is a reference of how to change a number to a character (see below) as you can see a = 97 in decimal equal 0110 0001 in binary. In code, you can ask to display a value in different formats (%d for decimal, %f for float, %c for char ...) but it is always a binary word in the computer

计算机如何区分类型?

Ask me if you want more information about low hardware layers

答案3

得分: 1

> 我在想我的电脑如何区分完全相同的一串0和1。

一般情况下,不同的指令会以不同的方式解释相同的比特序列。例如,x86架构的ADDL指令将其操作数解释为32位整数值,而ADDSD将其操作数解释为标量双精度浮点值。

您在源代码中指定的类型信息(intdoublechar等)决定了编译器生成的机器指令。例如,如果您有以下代码:

double a = 1.0, b = 2.0;
double c = a + b;

编译器将将其翻译为:

movsd -8(%rbp), %xmm0     // 将a的值(1.0)移动到xmm0寄存器
addsd -16(%rbp), %xmm0    // 将b的值(2.0)添加到xmm0中,将结果存储在xmm0中
movsd %xmm0, -24(%rbp)    // 将xmm0中的值(3.0)复制到c

如果您将double更改为int

int a = 1, b = 2;
int c = a + b;

然后编译器生成以下代码:

movl -4(%rbp), %edx   // 将a的值(1)移动到edx寄存器
movl -8(%rbp), %eax   // 将b的值(2)移动到eax寄存器
addl %edx, %eax       // 将edx中的值添加到eax中,将结果存储在eax中
movl %eax, -12(%rbp)  // 将eax中的值复制到c

对于您具体的问题:

> 我知道这与%c和%d有关,但实际上发生了什么?

整数值97以比特序列的形式存储 - 假设是8位类型,那么比特序列是01100001,或者0x61

%c基本上表示“将这个值呈现为基本字符集中对应的符号” - 换句话说,符号'a'。某处存在一个将整数值映射到相应符号的映射。

%d转换说明符基本上表示“创建这个值的十进制字符串表示” - 也就是说,发出字符序列{'9', '7'}

值如何映射到字符并显示取决于实现和终端驱动程序,这将因系统而异。

英文:

> I am wondering how my computer can make the difference between the exact same set of 0s and 1s.

In the general case, different instructions interpret the same sequence of bits differently. For example, the x86 ADDL instruction interprets its operands as 32-bit integer values, while ADDSD interprets its operands as scalar double-precision floating point values.

The type information you specify in your source code (int, double, char, etc.) determines what machine instructions the compiler generates. For example, if you have the code

double a = 1.0, b = 2.0;
double c = a + b;

the compiler will translate that to

movsd	-8(%rbp), %xmm0     // move value of a (1.0) to xmm0 register
addsd	-16(%rbp), %xmm0    // add value of b (2.0) to value in xmm0, store result in xmm0
movsd	%xmm0, -24(%rbp)    // copy value in xmm0 (3.0) to c

If you change double to int:

int a = 1, b = 2;
int c = a + b;

then the compiler generates the code:

movl	-4(%rbp), %edx   // move value of a (1) to edx register
movl	-8(%rbp), %eax   // move value of b (2) to eax register
addl	%edx, %eax       // add value in edx to eax, store result in eax
movl	%eax, -12(%rbp)  // copy value in eax to c

For your specific question, though:

> I know it is due to the %c and %d, but what is going on really ?

The integer value 97 is stored as a sequence of bits - assuming an 8-bit type, that bit sequence is 01100001, or 0x61.

The %c basically says "present this value as the corresponding symbol in the basic character set" - IOW, the symbol 'a'. Somewhere there's a mapping between integer values and corresponding symbols.

The %d conversion specifier basically says "create a decimal string representation of this value" - that is, emit the sequence of characters {'9', '7'}.

Exactly how values are mapped to characters and displayed is a function of the implementation and the terminal driver, and that will vary from system to system.

答案4

得分: -1

计算机不知道。您可以通过 printf 格式字符串中的 %c%d 来告诉它。

而且,这是这种特定体系结构使用ASCII码的约定,其中97代表字母a

您可以使用任何您喜欢的编码编写自己的输出函数。

英文:

Computer does not know. You tell him by %c and %din the printf format string.

And it is an conension used bt this particular architecture to use ASCII codes, where 97 is a

You can write your own output function using any coding you like.

huangapple
  • 本文由 发表于 2020年1月6日 21:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定