INF: How C Interprets Integer Constants with Leading Zeroes

ID Number: Q35037

3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

The assignments "a = 20" and "a = 020" return different results when

the values are printed. For example:

a = 20;

printf("%d", a); /* this prints "20" */

a = 020;

printf("%d", a); /* but this prints "16" */

More Information:

In Microsoft C, any number preceded by a 0 is handled as an octal

number (base 8). Therefore, "a = 20" is handled as you would expect;

however, "a = 020" is handled as an octal number and therefore

represents the decimal value 16.

Note also that ALL character constants of the form \o, \oo, \ooo, and

the string equivalents are always octal as well. (Hex character

constants start with "\x".) For instance, \33 or \033 are both the ESC

character (decimal 27, hex 1B). There is no way to use decimal numbers

to specify character constants; however, decimal integer constants can

be used instead (example: ch = 27;).

Additional reference words: 3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax