What a Leading Zero Really Costs You
A number written with a zero in front is not the number you think it is. In C, in Java, in Go and in pre-2015 JavaScript, 011 is a base-8 literal worth nine. Configuration files, CSV imports and hand-written test data are full of zero-padded values, and every one of them is a small trap. Paste the suspicious literal here and see what the compiler will actually store.
0755 equal to 7 × 64 + 5 × 8 + 5, which is 493 — not the seven hundred and fifty-five you might read at a glance.How the Bug Usually Arrives
Zero-Padded Data
Comparisons That Never Match
mode == 644 compares against 644 decimal and fails against the 420 the system holds.Knowing the decimal value also settles the opposite argument: if a literal is meant to be octal, seeing the base-10 number confirms it lands where you expect.
Testing a Suspicious Literal
Drop in the digits as written
Leading zeros are harmless here and any 0o marker loses its letter on the way in, so a Python literal can be pasted as it stands.
Compare with what you expected
If the number below differs from the digits you typed, the value is genuinely being read in base-8 and any comparison against a decimal constant is wrong.
Put the real value in the fix
The decimal field copies as a bare number with no marker, which is what a corrected comparison or a test assertion needs.
Leading Zeros in Source Code
The same characters mean different things depending on the language and the notation. This is what each literal is actually worth once it has been parsed.
| Written as | Read as | Decimal value | Note |
|---|---|---|---|
010 | Octal | 8 | C, Java, Go and legacy JavaScript |
011 | Octal | 9 | The classic off-by-two surprise |
0o10 | Octal | 8 | Explicit form in Python 3 and modern JavaScript |
0644 | Octal | 420 | A file mode, the number a script prints |
0755 | Octal | 493 | An executable mode |
01000 | Octal | 512 | Eight cubed, not a thousand |
Invalid Digits Cannot Sneak In
Typing 8 or 9 does nothing, the same refusal a compiler gives when a literal such as 09 turns out not to be legal octal.
The Gap Appears as You Type
Each keystroke updates the value, so you can watch the distance between what is written and what is stored grow digit by digit.
Evidence for the Code Review
A ?v= link opens with the literal already converted, which makes the point faster than a paragraph of explanation.
Questions About Octal Literals
Why does printing 011 in C give me 9?
The leading zero told the compiler the literal was base-8, so the digits mean one eight plus one. The variable holds nine and prints as nine; nothing is lost, it simply never was eleven.
What happened to this notation in Python 3?
It was removed on purpose. Writing 0644 is now a syntax error and you must write 0o644 instead, which makes the base impossible to miss when reading the code.
Is 09 a valid literal anywhere?
Not as octal — 9 is outside the digit range, so C rejects it and strict-mode JavaScript refuses the whole legacy form. Sloppier parsers may fall back to decimal, which is worse, because the meaning then depends on the tool.
Can a JSON file carry an octal number?
No. The format forbids a leading zero altogether, so a mode has to travel as a decimal integer or as a quoted string. That is exactly why a permissions field so often arrives as 420.
Does a zero-padded string behave the same way?
A quoted "011" stays text until something parses it, and most modern parsers then read it as decimal eleven. The danger is the unquoted literal in source, where the base is decided at compile time.
No comments yet. Be the first to comment!