I'm not sure it's in C specification, but a quick check with gcc 8.3 shows it's an error, at least as far as that compiler is concerned: https://ideone.com/8GJa5m
#include <stdio.h>
int main(void)
{
printf("%d", 018 - 015);
return 0;
}
prog.c: In function ‘main’:
prog.c:5:15: error: invalid digit "8" in octal constant
printf("%d", 018 - 015);
^~~
right, the treatment of invalid octal literals is a javascript (or rather, early implementations of javascript, just like typeof null) fuckup. but thankfully it doesn't matter in modern code because of strict mode.
I would argue that C did it wrong too and that something like 8x would have been better, or really anything that doesn't appear in base 10 or the numbering system that it is being used to represent.
If you blindly copy this mistake, you are still making a mistake. Rust uses 0o, for example
But C also always threw for this (or, well, since 1989, so 6 years before javascript), so javascript made yet another mistake on top of that of not learning from worst part of the mistake that C corrected 6 years prior.
To be clear, I see you also think this, which is why you included "unlucky" in your comment, but I figured it was worth stating.
View all comments
Show parent comment
12
u/inoffensiveLlama 19d ago
It makes sense the way you explained it. Still… I wish I could unknow what I have just learned…