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

324 views Asked by At

In the following C code are octal literals used for all these defines? Even if they start with multiple zeros?

#define TCL_REG_BASIC       000000  /* BREs (convenience). */
#define TCL_REG_EXTENDED    000001  /* EREs. */
#define TCL_REG_ADVF        000002  /* Advanced features in EREs. */
#define TCL_REG_ADVANCED    000003  /* AREs (which are also EREs). */
#define TCL_REG_QUOTE       000004  /* No special characters, none. */
#define TCL_REG_NOCASE      000010  /* Ignore case. */
#define TCL_REG_NOSUB       000020  /* Don't care about subexpressions. */
#define TCL_REG_EXPANDED    000040  /* Expanded format, white space & comments. */
#define TCL_REG_NLSTOP      000100  /* \n doesn't match . or [^ ] */
#define TCL_REG_NLANCH      000200  /* ^ matches after \n, $ before. */
#define TCL_REG_NEWLINE     000300  /* Newlines are line terminators. */
#define TCL_REG_CANMATCH    001000  /* Report details on partial/limited * matches. */
4

There are 4 answers

2
Taylor Brandstetter On BEST ANSWER

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

1
paulsm4 On

Yes.

http://www.cplusplus.com/doc/tutorial/constants/

In addition to decimal numbers (those that most of us use every day), C++ allows the use of octal numbers (base 8) and hexadecimal numbers (base 16) as literal constants. For octal literals, the digits are preceded with a 0 (zero) character. And for hexadecimal, they are preceded by the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other:

75         // decimal
0113       // octal
0x4b       // hexadecimal  
11
haccks On

Yes You are right.

C11, 6.4.4.1 Integer constants:

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

0
chux - Reinstate Monica On

OP's examples are all octal as they begin with a 0. Multiple leading zeros or the presence of an 8 or 9 does not change it away from being octal. An 8 or 9 should case an error.


FYI: Some additional occurrences (though maybe not a literal) in C where decimal, octal and hexadecimal interpretation occur.

Although not a literal, in a format string for printf(), specifiers like "%016llx" are not an octal 016 width, but a flag '0' and a decimal value of 16.

With printf(), output using specifier "%a", the output is in the style "[−]0xh.hhhh p±d," where the significand is in hexadecimal and the exponent is in decimal.

In escape sequences there is no decimal specification. Some samples follow:

\' \" \? \\ \a \b \f \n \r \t \v
\0   (octal)
\01   (octal)
\012   (octal)
\0123   (bad - only up to 3)
\x0 (hexadecimal)
\x01 (hexadecimal)
\x012 (hexadecimal)
\x0123 (hexadecimal)
\x01234… (hexadecimal)
\u1234 (hexadecimal)
\U00012345 (hexadecimal)