Skip to content

Octal Literals

Back when computers had limited address spaces, octal numbers were handy. Each digit represents 3 bits and a short string of octal can represent a 16-bit or 24-bit address. Octal numbers were so handy that programming languages like C included octal literals. The syntax is simple: a numeric literal with a leading ‘0’ and no decimal point is an octal literal. For example, 0777 is the octal representation of 511 decimal.

As address spaces expanded to 32-bits and beyond, octal syntax was replaced with hexidecimal. The transition happened more than ten years ago. Octal literal syntax is unknown to many developers today and can cause unintentional errors. For example:

int powersOfTen[] = {
  0001, //okay
  0010, // error: this is 8, not 10
  0100, // error: this is 64, not 100
  1000, // okay

Given that octal isn’t that useful today, would you expect that any recently developed programming language to support them in the same way as C/C++? Probably not but many do. Java does. So does Javascript. And Ruby. And Perl. And Python (although Python 3 has adopted a less error-prone syntax). Why were C-style octal literals carried over into these languages? Probably because their interpreters/compilers were initially written in C/C++ and old habits (and syntax) die hard.

Some languages have bucked this trend. Smalltalk for example, uses an explicit radix prefix for octal and hexidecimal (e.g. 8r377 and 16r7FFF). And C# does not support octal literals at all.

Posted in Uncategorized.

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. Silicon Valley Code Camp 2011 linked to this post on October 16, 2011

    […] eval. Here’s why it’s evil. *Cough* code injection!No more octal literals. Not needed since punch card days.Forgetting to use the new prefix will now throw an exception, not silently […]

Some HTML is OK

or, reply to this post via trackback.