views:

910

answers:

3

Why are Hexadecimal Prefixed as 0x and not anything else? I understand the usage of prefix but I dont understand the significance of 0x.

+2  A: 

It appears to have stemmed from a C convention: http://en.wikipedia.org/wiki/Hexadecimal#Representing_hexadecimal .

wsh
-1. The article merely states that C uses `0x`. But we already knew that. The question is *why*. Wikipedia gives no reason, and it says nothing about it being a convention.
Rob Kennedy
Examine the tags of the question itself; presumably the generalized reference has to do with the predominance of Unix and C. Furthermore, it seems that before C this was not a way to refer to hex numbers.
wsh
Right. The tags (added by Roygbiv, not Kunjaan) tell us the question is about C and other languages. In other words, the question asks, "Why are hexadecimals prefixed with `0x` in C, C++, and C#?" And your answer is to say that C uses `0x`? You're adding no new information; you're just begging the question. And if `0x` wasn't used prior to C, then that's all the more reason to wonder why it was done that way in C.
Rob Kennedy
Okay, fair enough.
wsh
@Rob: actually, the question is "what does `0x` mean"; it's simply a prefix indicating that what follows is an integer constant in hex format. As to *why* `0x` over some other prefix, I suspect it was a totally arbitrary decision; there's certainly nothing in the standard or in any of my other references as to why that sequence was used as opposed to something else.
John Bode
+13  A: 

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:

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.

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.

0xFF00 // definitely not an identifier!
AshleysBrain
Interesting. I imagine they could have used a leading 0 AND trailing h to denote hex. The trailing h probably would have been confused with the type specifier suffix, e.g. 0xFF00l vs 0FF00hl
zdan
This argument implies that the use of a leading zero to denote octal numbers predates the use of the hexadecimal "0x" prefix. Is this true?
Andreas Rejbrand
Wouldn't they have both been invented at the same time? Why would there ever be one but not the other?
AshleysBrain
A: 

0x simply indicates that the following digits represent an integer value in hex notation (base 16). The grammar is

hexadecimal-constant:
    0x hex-digit
    0X hex-digit
    hexadecimal-constant hex-digit

where

hex-digit: one of
    0 1 2 3 4 5 6 7 8 9
    A B C D E F a b c d e f

Thus, 0x10 means "interpret the integer constant 10 in base 16, not base 10".

As to why C uses 0x as opposed to some other indicator, I suspect that was an entirely arbitrary decision by the language designers. It's consistent with using a leading 0 to indicate an octal constant. I haven't found a rationale for it in any of my references or the language standard (although I haven't looked all that hard).

John Bode