Is 0 a Decimal Literal or an Octal Literal

Is 0 a decimal literal or an octal literal?

Yes, 0 is an Octal literal in C++.

As per the C++ Standard:

2.14.2 Integer literals [lex.icon]

integer-literal:  
decimal-literal integer-suffixopt
octal-literal integer-suffixopt
hexadecimal-literal integer-suffixopt
decimal-literal:
nonzero-digit
decimal-literal digit
octal-literal:
0 <--------------------<Here>
octal-literal octal-digit

Is 0 an octal or a decimal in C?

It makes little difference, but formally the integer constant 0 is octal in C. From the C99 and C11 standards, 6.4.4.1 Integer constants

integer-constant:

    decimal-constant integer-suffixopt

    octal-constant integer-suffixopt

    hexadecimal-constant integer-suffixopt

decimal-constant:

    nonzero-digit

    decimal-constant digit

octal-constant:

    0

    octal-constant octal-digit

hexadecimal-constant:

    ...

    ...

Why JavaScript treats a number as octal if it has a leading zero

I think my answer here answers the question, but the question is not exactly a duplicate, so I include a copy of my answer.

History

The problem is that decimal integer literals can't have leading zeros:

DecimalIntegerLiteral ::
0
NonZeroDigit DecimalDigits(opt)

However, ECMAScript 3 allowed (as an optional extension) to parse literals with leading zeros in base 8:

OctalIntegerLiteral ::
0 OctalDigit
OctalIntegerLiteral OctalDigit

But ECMAScript 5 forbade doing that in strict-mode:

A conforming implementation, when processing strict mode code (see
10.1.1), must not extend the syntax of NumericLiteral to include OctalIntegerLiteral as described in B.1.1.

ECMAScript 6 introduces BinaryIntegerLiteral and OctalIntegerLiteral, so now we have more coherent literals:

  • BinaryIntegerLiteral, prefixed with 0b or 0B.
  • OctalIntegerLiteral, prefixed with 0o or 0O.
  • HexIntegerLiteral, prefixed with 0x or 0X.

The old OctalIntegerLiteral extension has been renamed to LegacyOctalIntegerLiteral, which is still allowed in non-strict mode.

Conclusion

Therefore, if you want to parse a number in base 8, use the 0o or 0O prefixes (not supported by old browsers), or use parseInt.

And if you want to be sure your numbers will be parsed in base 10, remove leading zeros, or use parseInt.

Examples

  • 010

    • In strict mode (requires ECMAScript 5), it throws.
    • In non strict mode, it may throw or return 8 (implementation dependent).
  • 0o10, 0O10

    • Before ECMAScript 6, they throw.
    • In ECMAScript 6, they return 8.
  • parseInt('010', 8)

    • It returns 8.
  • parseInt('010', 10)

    • It returns 10.

Why does Go Programming treat any number starting with zero as an octal and how can I prevent this?

Even though Go 1.13 introduced a change in the integer literals, your int would still be interpreted as octal (which cannot have '9' in it, hence the error message)

Octal integer literals: The prefix 0o or 0O indicates an octal integer literal such as 0o660.

The existing octal notation indicated by a leading 0 followed by octal digits remains valid.

Any Go library dealing with phone number would store it as string.

And that data can be more detailed that one string.

For instance dongri/phonenumber would follow the ISO 3166 COUNTRY CODES standard, with a struct like:

type ISO3166 struct {
Alpha2 string
Alpha3 string
CountryCode string
CountryName string
MobileBeginWith []string
PhoneNumberLengths []int
}

That is safer than an int, and offer a better validation.

Is 00 an integer or octal in Java?

All are integers, but...

1  is decimal
0 is decimal
01 is octal
00 is octal

From Java Language Specification (emphasis mine):

Note that octal numerals always consist of two or more digits; 0 is always considered to be a decimal numeral - not that it matters much in practice, for the numerals 0, 00, and 0x0 all represent exactly the same integer value.

Octal number literals: When? Why? Ever?

I recently had to write network protocol code that accesses 3-bit fields. Octal comes in handy when you want to debug that.

Just for effect, can you tell me what the 3-bit fields of this are?

0x492492

On the other hand, this same number in octal:

022222222

Now, finally, in binary (in groups of 3):

010 010 010 010 010 010 010 010

Using C am I right in thinking that literals beginning with multiple zeros are considered octal?

From C Standard, 6.4.4.1 Paragraph 3:

An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only

Why are integer literals with leading zeroes interpreted strangely?

A leading zero denotes that the literal is expressed using octal (a base-8 number).

0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3), which equals 83 in decimal.
For some reason, octal floats are not available.

Just don't use the leading zero if you don't intend the literal to be expressed in octal.

There is also a 0x prefix which denotes that the literal is expressed in hexadecimal (base 16).

Data types in Java

An integer literal with a leading 0 is an octal literal, and 8 isn't a valid digit within octal literals.

If you actually want a value of 786 decimal, just remove the leading 0:

double a = 786;


Related Topics



Leave a reply



Submit