3

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. */
haccks
  • 104,019
  • 25
  • 176
  • 264
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199

4 Answers4

4

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

Taylor Brandstetter
  • 3,523
  • 15
  • 24
4

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.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Out of curiosity what disallows decimals literals with leading zeroes? E.g. 08 is a syntax error as far as GCC and MSVC is concerned. – doynax Dec 16 '13 at 21:48
  • @doynax; Since when `8` comes to the octal representation of a number? And why compiler should not give the warning? I suggest you to read number system. – haccks Dec 16 '13 at 21:54
  • I was just curious what the rule identifying 08 as an invalid octal number rather than a proper decimal literal might be since you seemed to have the standard handy. No real reason, just the inner lawyer in me wanting to pick holes in any formal definitions. – doynax Dec 16 '13 at 22:01
  • @doynax the same section 6.4.4.1 of the standard specifically defines a decimal constant as starting with a non-zero decimal digit. – Nigel Harper Dec 16 '13 at 22:03
  • 2
    @Nigel Harper: Damn. And I suppose the sequence of 0-7 digits here may be empty, thus covering the "0" case as a (very common) octal literal – doynax Dec 16 '13 at 22:06
  • I am pissed off by downvoting without any explanation/comment. This is cowardice. – haccks Dec 16 '13 at 22:07
  • @doynax Exactly - the "optionally followed" bit in the standard quote means that a bare 0 is technically a valid octal constant. – Nigel Harper Dec 16 '13 at 22:12
  • 1
    @doynax; Here is the complete para: `A decimal constant begins with a nonzero digit and consists of a sequence of decimal digits. An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only. A hexadecimal constant consists of the prefix 0x or 0X followed by a sequence of the decimal digits and the letters a (or A) through f (or F) with values 10 through 15 respectively.` Which clearly suggest that a decimal number can't begins with `0`. Continued........ – haccks Dec 16 '13 at 22:13
  • @doynax; .....This means that [a bare `0` is not a decimal integer but an octal integer](http://stackoverflow.com/a/6895543/2455888). – haccks Dec 17 '13 at 12:44
  • @haccks: Indeed, and Nigel concurs. Thankfully octal numbers have no other properties compared to decimal ones (aside from unsigned promotion) and are thus indistinguishable in this case as near as I can tell – doynax Dec 17 '13 at 13:09
  • @doynax; I commented regarding your comment:`I was just curious what the rule identifying 08 as an invalid octal number rather than a proper decimal literal might be since you seemed to have the standard handy. No real reason, just the inner lawyer in me wanting to pick holes in any formal definitions` – haccks Dec 17 '13 at 13:11
0

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  
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

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)
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256