Re: Error in 'man operator'
From: David Sletten <hidden>
Date: 2021-08-28 00:08:52
All right. I knew I wasn't on completely solid ground since I don't have the standard. Thanks for checking. On 8/27/21 8:01 PM, Alejandro Colomar (man-pages) wrote:
Hi David, On 8/28/21 1:39 AM, Alejandro Colomar (man-pages) wrote:quoted
Hi, On 8/27/21 8:11 PM, David Sletten wrote:quoted
Hi, I've run across what I believe is a minor error in the man page for C operators (https://man7.org/linux/man-pages/man7/operator.7.html ). The man page lists casting in the 2nd row of the table: Operator Associativity Notes () [] -> . ++ -- left to right [1] ! ~ ++ -- + - (type) * & sizeof right to left [2] * / % left to right However, in "C A Reference Manual" (5th ed.), table 7-3 on pg. 205 shows that all of the operators in row 2 above have a precedence of 15 except for casts which have a precedence of 14. Consequently, the man page should display casts as the (new) 3rd row in the table above. Thanks for your effort in maintaining this documentation.Reviewing the standard (C18), "C A Reference Manual" seems to be right. Unary operators are mentioned in C18::6.5.3, and casts are in C18::6.5.4 (they are mentioned in order of precedence). I also noticed that we're missing compound literals in the first row. And we're also missing _Alignof() in the second row. However, since all of those use parentheses (and braces), there's no doubt in their precedence, so I'll ignore them for now. And for some reason they are in a different order from the one in the standard, which makes it a bit difficult to correlate what is in the manual page and what is in the standard, so I think reordering them inside the rows will also be an improvement.There seems to be a note in the standard that implies that casts have effectively the same precedence as the unary operators (I don't know why they would have decided to define it in a separate major subsection; maybe it was easier to write the differences in that way). See the standard: (Note 86 in C18::6.5):quoted
86)The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of themajor subclauses of this subclause, highest precedence first. Thus, for example, the expressions allowed as the operandsof the binary+operator (6.5.6) are those expressions defined in 6.5.1 through 6.5.6. The exceptions are cast expressions(6.5.4) as operands of unary operators (6.5.3), and an operand contained between any of the following pairs of operators:grouping parentheses()(6.5.1), subscripting brackets[](6.5.2.1), function-call parentheses()(6.5.2.2), and the conditionaloperator?:(6.5.15).Within each major subclause, the operators have the same precedence. Left- or right-associativity is indicated in eachsubclause by the syntax for the expressions discussed thereinThat makes sense, since the cast operator really is unary (it applies to a "unary expression") and since it and all of the so-called unary operators and right-to-left associativity, I can't imagine of an expression where that different precedence would matter. I mean: !(int)x (int)!x The precedence doesn't matter there, as they apply one after the other, right to left. So, I'll keep casts in row 2. Cheers, Alex
-- Have all good days! David Sletten