Sunday 20 November 2011

What is octal? in C programming

What is octal?

Octal is base 8. Oh, no, another numbering system? Unfortunately, yes. But there is no need to describe base 8 to the level of detail that was described for base 2. To count in octal, you count 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, and so on. The following two lines count in base 8 and in base 10 side by side for comparison purposes (base 10 is on top):

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20

Notice that in base 8 (on bottom), you had to increase to two decimal, I mean octal, places after you reached
7. The second octal place is of course 81 (which is equal to 8 in the decimal system). If you were to continue counting to three octal places (100 in octal), that would be 82, or 64 in decimal. Therefore, 100 in octal is equal to 64 in decimal.
 
Octal is not as frequently used these days as it once was. The major reason is that today’s computers are 8-, 16-, 32-, or 64-bit processors, and the numbering system that best fits those is binary or hexadecimal (see FAQ XX.24 for more information on the hexadecimal numbering system). 

The C language supports octal character sets, which are designated by the backslash character (\). For
example, it is not uncommon to see C code that has the following statement in it:

if(x == ‘\007’) break;
 
The \007 happens to also be decimal seven; the code is checking for the terminal beep character in this case.
Another common number denoted in octal is \033, which is the Escape character (it’s usually seen in code as \033). However, today you won’t see much of octal—hexadecimal is where it’s at.

Cross Reference:

XX.22: What is binary?
XX.24: What is hexadecimal?

No comments:

Post a Comment