C语言 为什么十六进制数字以 0x 为前缀?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2670639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are hexadecimal numbers prefixed with 0x?
提问by unj2
Why are hexadecimal numbers prefixed as 0x?
I understand the usage of the prefix but I don't understand the significance of why 0xwas chosen.
为什么十六进制数字的前缀为0x?我了解前缀的用法,但我不明白为什么0x选择的重要性。
回答by ?r?ola
Short story:The 0tells the parser it's dealing with a constant (and not an identifier/reserved word). Something is still needed to specify the number base: the xis an arbitrary choice.
简单地说:在0告诉它在处理一个恒定的(而不是一个标识符/保留字)的解析器。仍然需要一些东西来指定数字基础:这x是一个任意选择。
Long story:In the 60's, the prevalent programming number systems were decimal and octal— mainframes had 12, 24 or 36 bits per byte, which is nicely divisible by 3 = log2(8).
长话短说:在 60 年代,流行的编程数字系统是十进制和八进制——大型机每字节有 12、24 或 36 位,可以很好地被 3 = log2(8) 整除。
The BCPL language used the syntax 8 1234for octal numbers. When Ken Thompson created B from BCPL, he used the 0prefix instead. This is great because
BCPL 语言使用8 1234八进制数的语法。当 Ken Thompson 从 BCPL 创建 B 时,他使用了0前缀。这很棒,因为
- an integer constant now always consists of a single token,
- the parser can still tell right away it's got a constant,
- the parser can immediately tell the base (
0is the same in both bases), - it's mathematically sane (
00005 == 05), and - no precious special characters are needed (as in
#123).
- 一个整数常量现在总是由一个标记组成,
- 解析器仍然可以立即知道它有一个常数,
- 解析器可以立即告诉基数(
0两个基数相同), - 它在数学上是合理的 (
00005 == 05),并且 - 不需要珍贵的特殊字符(如
#123)。
When C was created from B, the need for hexadecimal numbers arose (the PDP-11 had 16-bit words) and all of the points above were still valid. Since octals were still needed for other machines, 0xwas arbitrarily chosen (00was probably ruled out as awkward).
当从 B 创建 C 时,出现了对十六进制数的需求(PDP-11 有 16 位字),并且上述所有要点仍然有效。由于其他机器仍然需要八进制,0x因此随意选择(00可能被排除为尴尬)。
C# is a descendant of C, so it inherits the syntax.
C# 是 C 的后代,因此它继承了语法。
回答by AshleysBrain
Note: I don't know the correct answer, but the below is just my personal speculation!
注:我不知道正确答案,但以下只是我个人的猜测!
As has been mentioned a 0 before a number means it's octal:
如前所述,数字前的 0 表示它是八进制的:
04524 // octal, leading 0
Imagine needing to come up with a system to denote hexadecimal numbers, and note we're working in a C style environment. How about ending with h like assembly? Unfortunately you can't - it would allow you to make tokens which are valid identifiers (eg. you could name a variable the same thing) which would make for some nasty ambiguities.
想象一下需要想出一个系统来表示十六进制数,并注意我们在 C 风格的环境中工作。以 h like assembly 结尾怎么样?不幸的是你不能 - 它会允许你制作作为有效标识符的标记(例如,你可以将一个变量命名为相同的东西),这会导致一些令人讨厌的歧义。
8000h // hex
FF00h // oops - valid identifier! Hex or a variable or type named FF00h?
You can't lead with a character for the same reason:
出于同样的原因,您不能以角色为首:
xFF00 // also valid identifier
Using a hash was probably thrown out because it conflicts with the preprocessor:
使用散列可能会被丢弃,因为它与预处理器冲突:
#define ...
#FF00 // invalid preprocessor token?
In the end, for whatever reason, they decided to put an x after a leading 0 to denote hexadecimal. It is unambiguous since it still starts with a number character so can't be a valid identifier, and is probably based off the octal convention of a leading 0.
最后,无论出于何种原因,他们决定在前导 0 后面放一个 x 来表示十六进制。它是明确的,因为它仍然以数字字符开头,因此不能是有效标识符,并且可能基于前导 0 的八进制约定。
0xFF00 // definitely not an identifier!
回答by loyola
It's a prefix to indicate the number is in hexadecimal rather than in some other base. The C programming language uses it to tell compiler.
它是一个前缀,表示数字是十六进制而不是其他基数。C 编程语言使用它来告诉编译器。
Example:
例子:
0x6400translates to 6*16^3 + 4*16^2 + 0*16^1 +0*16^0 = 25600.When compiler reads 0x6400, It understands the number is hexadecimal with the help of 0xterm. Usually we can understand by (6400)16or (6400)8or whatever ..
0x6400翻译成 6*16^3 + 4*16^2 + 0*16^1 +0*16^0 = 25600.当编译器读取时0x6400,它会在0x项的帮助下理解数字是十六进制的。通常我们可以通过 (6400) 16或 (6400) 8或其他什么来理解..
For binaryit would be:
对于二进制,它将是:
0b00000001
0b00000001
Hope I have helped in some way.
希望我在某种程度上有所帮助。
Good day!
再会!
回答by Johnny Low
The preceding 0 is used to indicate a number in base 2, 8, or 16.
前面的 0 用于表示以 2、8 或 16 为基数的数字。
In my opinion, 0x was chosen to indicate hex because 'x' sounds like hex.
在我看来,选择 0x 来表示十六进制是因为“x”听起来像十六进制。
Just my opinion, but I think it makes sense.
只是我的意见,但我认为这是有道理的。
Good Day!
再会!

