r/ProgrammerHumor • • 19d ago

Meme whoWouldHaveThought

Post image
5.7k Upvotes

626 comments sorted by

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…

7

u/the_horse_gamer 19d ago

it's a C feature that a few unlucky languages inherited. it's not actually possible in javascript strict mode (which most modern code runs in).

5

u/Zdrobot 19d ago

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);
               ^~~

1

u/the_horse_gamer 19d ago

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.

1

u/inoffensiveLlama 19d ago

Oh, so this would be the same in C?

6

u/CMDR_ACE209 19d ago

Of course not. Accepting invalid octal literals and treating them as decimal is absolutely not a C feature.

1

u/no_brains101 18d ago edited 18d ago

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.