A Forgotten 'Feature' from Pre-ANSI C
Description
The image displays a snippet of text from what appears to be a technical document, likely an old C language specification. The text is formatted with section numbers. Section 2.3.1 defines integer constants and notes that an integer is treated as octal if it starts with a 0. The most notable and underlined sentence states: "The digits 8 and 9 have octal value 10 and 11 respectively." A watermark at the bottom right says "More fun at https://t.me/dev_meme". This content is humorous to experienced developers because it highlights a bizarre and counter-intuitive quirk from a very early version of the C programming language (pre-ANSI). In modern C and almost all other languages, octal numbers can only contain digits from 0 to 7. The idea that a parser would accept an invalid octal digit like '8' and interpret it as the octal value '10' is a perfect example of the dangerous and unpredictable behavior that existed in early, less-standardized programming languages
Comments
59Comment deleted
Some say this is why you can't trust legacy code. The truth is, you can't even trust the legacy compiler that wrote the legacy code
Somewhere an old K&R-era lexer just shrugged and said, “well, *technically* that’s your problem at link-time.”
This is the kind of language design decision that makes you wonder if Dennis Ritchie was actively trying to create job security for C programmers, or if Bell Labs just had a really aggressive April Fools' culture that got out of hand
Ah yes, the legendary octal system where 8 and 9 exist - clearly written by someone who thought 'leading zero means octal' was more of a suggestion than a rule. This is the programming equivalent of saying 'the letter Ω has ASCII value 65.' It's the kind of spec that makes you wonder if the manual was compiled with `-Wno-math` and whether the author ever actually *ran* their code, or just shipped the documentation and called it a day. No wonder we have off-by-one errors - we can't even agree on which digits exist in which base
Nothing ages a codebase faster than a leading zero - 09 becomes a postmortem while someone explains why chmod 0755 is fine but 0855 isn’t
When docs treat octal like base-12: digits 8=10, 9=11, and your compiler's undefined behavior becomes the real constant
Legacy C: where 010 equals 8, 011 equals 9, and 09 equals a postmortem (bonus points if your YAML parser agrees)
Hahahhahahahgahaha typical C Comment deleted
"pre-ansi C" just means various C dialects from before it was standardized. In many ways, this is the exact opposite of "typical C" Comment deleted
I mean the overall feeling of the language, where shooting yourself in the leg is almost inevitable Comment deleted
that's more C++ C is primitive, but it's also possible to know everything there is to know, reducing errors from ignorance Comment deleted
imo of course Comment deleted
I don't think this fragment means what you mean Comment deleted
In another edition of this reference this fragment is a bit longer (and surounding context makes it clear) https://www.livingcomputers.org/UI/UserDocs/Unix-v7-1/UNIX_Programmers_Manual_Seventh_Edition_Vol_2_1983.pdf Comment deleted
yeah and octal value of 10 is 8 Comment deleted
and octal value of 11 is 9, so what's the problem? Comment deleted
What you are taking is notation (binary/hex/etc) And no, it’s not like this in (almost) any language of any standard For example let’s take just two most popular languages: JS and Go (but it’s ~same in the rest of popular languages) How do you define octal number? Eg: 0123 - is definition of Octal number thanks to leading 0 Octal number 123 in decimal is 83. Try it in your browser right now Comment deleted
and this extended fragment with similar (albeit, less vague) note about hex makes me even more confident Comment deleted
you got it wrong they just mean that 8 is 10 in octal not that 8 is interpreted as octal right away. just poor explanation Comment deleted
it is exactly the same in every language even by today Comment deleted
there is no contradiction to what is written in this text Comment deleted
It is Comment deleted
they say octal notation begins with leading 0 that is true here 8 is VALUE of octal 10 —> 010 9 is VALUE of octal 11 —> 011 Comment deleted
yes, because there is no octal 08 Comment deleted
You have misunderstanding of what is notation and how notaions are interpreted by languages Comment deleted
dude Comment deleted
it doesn't exist and will be converted as decimal Comment deleted
Weirdness highligthed in the bell’s standart is that 8 and 9 was considered to be valid octals Comment deleted
is it so? Comment deleted
But not in the Bell’s standart Comment deleted
well, then. you are right Comment deleted
but it is still the same. 08 = 010 = 8 Comment deleted
Maybe it will be easier to get the point by adding 2 more lines? Comment deleted
08 = 010 = 8 09 = 011 = 9 Comment deleted
Yeah, so weirdness is that per Bell’s standart there’s 2 ways to define decimal numbers 8 and 9 using octal notation Comment deleted
yes. that is where js is kinda the same as C it tries to interpret your value somehow even if it's bullshit Comment deleted
Don’t say that you want runtime error when number has leading zero 😄 Comment deleted
well, you shouldn't write it like this in a first place, but still if you do, you will get correct result. Comment deleted
Most of data is not what we wrote but the data which we got somehow in the process Comment deleted
But allowing language (per standart) to interpret 8 and 9 as valid octals is… wtf? 😄 Comment deleted
I know where this is handy, but it's a topic too big for this chat Comment deleted
Sure there’s a cases and that’s why no one here is blaming Bell’s engineers for that choice. But it might be a very hard issue to debug when you see numbers with 8 and 9 being received from the user and then somehow during execution it becomes much smaller than it was initially Comment deleted
And hey, that’s C! It wouldn’t take much time to make your octals which would work like in Bell’s standart. But that’s not a common case and not how octals should work in general Comment deleted
yeah, you got the point. but they had it more comfortable this way. it still doesn't break any logic. en contraire, it fixes some common input errors automatically. see no problem in that Comment deleted
Otherwise, just as continuation of that idea, we will end up with numbers like 0b12345 being valid binary Comment deleted
not in this case. here it is a mere representation inside the code underneath you have just binary Comment deleted
in case of input it is either binary straight away, so it will be correctly interpreted anyways. or it is a string, and there you force a way to interpret it, but this is the point. string input can be sick, and this approach helps to fix some of the common errors. if that isn't wanted, you always can validate the input prior to interpretation yourself. Comment deleted
I understood your point. But now if we’re talking about definition of notations in standarts it would be weird that only octals are allowed “overflow” number to be used as valid ones. After this we will have to make one of 3 main decisions: 1) Keep it only for octals (for historical purpose, lol) 2) Allow all notations to use “overflow” number 3) Remove that approach from octals so they act as number of other notations Comment deleted
But it’s not just JS. I used it to show example as the easier way to experiment Comment deleted
I know Comment deleted
not in decimal in octal Comment deleted
9 and 10 doesn’t exist in octal notation Comment deleted
but you still get correct result Comment deleted
I do C for about 12 years. Comment deleted
that is, well, wonderful I would say Comment deleted
I have to confess that I am a bit drunk, but I don't see any convincing arguments in last ~50 messages supporting thesis that "pre-ansi C recognized 8 and 9 as octal digits" Comment deleted
"The digits 8 and 9 have octal value 10 and 11" just seems to be a vague "explanation" (more like confirmation that it works the way one would think it does) how octal works Comment deleted
I think that if "8 and 9 are valid octal digits" statement were true, it would be noted more explicitly Comment deleted
Those seem to be examples. If you need 8, please type 010. If you need 9, please type 011 . Comment deleted