Octal Numerals

A literal integer preceded by 0 (zero) is an octal numeral. When using octal numbers, you should beware of the following common pitfall:

 const int warning = 10;const int error = 100, switch (status) {case 010   // OOPS, decimal value is 8, not 10   handle_warning ();  // unreachable code!   break;case 100:   handle_error ();   break;default:   break;}

In this example, the programmer intended to format the case-labels in a uniform, fixed size of thee digits. However, the compiler treated each zero-preceded label as an octal number rather than a decimal one.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: