Skip to contentSkip to conversation

Error in 'man operator'

DISCUSSION David Sletten 3 participants 9 messages Aug 27, 2021 – Aug 28, 2021 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

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.

Signature · 2 lines
Have all good days!
David Sletten
#1 lore raw

Hi,

Quoted 6 lines · David Sletten

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:

Quoted 9 lines

       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.

Thanks,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#2 lore raw

Hi David,

Quoted 23 lines · Alejandro Colomar (man-pages)

Hi,

Quoted 6 lines · David Sletten

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:

Quoted 9 lines

        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).

Quoted 5 lines

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 1 line

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 therein

That 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

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#3 lore raw

All right. I knew I wasn't on completely solid ground since I don't have the standard. Thanks for checking.

Quoted 51 lines · Alejandro Colomar (man-pages)

Hi David,

Quoted 19 lines · Alejandro Colomar (man-pages)

Hi,

Quoted 6 lines · David Sletten

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:

Quoted 9 lines

        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.

Quoted 7 lines

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 1 line

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 therein

That 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

Signature · 2 lines
Have all good days!
David Sletten
#4 lore raw

Hi David,

Quoted 1 line · David Sletten

All right. I knew I wasn't on completely solid ground since I don't have the standard. Thanks for checking.

You can find all of the C and C++ standards easily here:

<https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents>

And a direct link to the C18 standard (actually a draft, but for most things you'll need, it's enough):

<https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf>

Cheers,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#5 lore raw
In reply to Alejandro Colomar (man-pages)

Hi David,

Quoted 7 lines · Alejandro Colomar (man-pages)

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.

Hmm, I just came up with some very stupid piece of code that can show the different precedence between "unary operators" and "cast operators":

sizeof(int)x;

Which I hope no-one on earth would ever want to be valid C.

If casts had the same precedence as unary operators, this would be:
First, cast x to int, then apply sizeof to the resulting int (since sizeof can be used without parentheses on non-types).

But since sizeof has greater precedence than the cast, it would be:
First, calculate sizeof(int), then... x? Invalid expression!

And luckily, the second thing happens, and the compiler yells:

sizeof.c: In function ‘foo’:
sizeof.c:3:20: error: expected ‘;’ before ‘x’

    3 |  return sizeof(int)x;
      |                    ^
      |                    ;

So I think this deserves a new row.

Thanks!

Alex

Quoted 5 lines

Cheers,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#6 lore raw

Ha. Nice work.

Quoted 45 lines · Alejandro Colomar (man-pages)

Hi David,

Quoted 7 lines · Alejandro Colomar (man-pages)

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.

Hmm, I just came up with some very stupid piece of code that can show the different precedence between "unary operators" and "cast operators":

sizeof(int)x;

Which I hope no-one on earth would ever want to be valid C.

If casts had the same precedence as unary operators, this would be:
First, cast x to int, then apply sizeof to the resulting int (since sizeof can be used without parentheses on non-types).

But since sizeof has greater precedence than the cast, it would be:
First, calculate sizeof(int), then... x?  Invalid expression!

And luckily, the second thing happens, and the compiler yells:

sizeof.c: In function ‘foo’:
sizeof.c:3:20: error: expected ‘;’ before ‘x’
    3 |  return sizeof(int)x;
      |                    ^
      |                    ;

So I think this deserves a new row.

Thanks!

Alex

Quoted 5 lines

Cheers,

Alex

Signature · 2 lines
Have all good days!
David Sletten
#7 lore raw
In reply to Alejandro Colomar (man-pages)

* Alejandro Colomar <[email protected]>, 2021-08-28, 02:17:

Quoted 3 lines

Hmm, I just came up with some very stupid piece of code that can show the different precedence between "unary operators" and "cast operators":

sizeof(int)x;

Perhaps a more vivid example would be:

sizeof (int) + 1

which is parsed as

(sizeof (int)) + 1

rather than:

sizeof ((int) +1)

Signature · 1 line
Jakub Wilk
#8 lore raw

Hi Jakub,

Quoted 7 lines · Jakub Wilk

* Alejandro Colomar <[email protected]>, 2021-08-28, 02:17:

Quoted 3 lines

Hmm, I just came up with some very stupid piece of code that can show the different precedence between "unary operators" and "cast operators":

sizeof(int)x;

Perhaps a more vivid example would be:

Quoted 8 lines

  sizeof (int) + 1

which is parsed as
  (sizeof (int)) + 1

rather than:

  sizeof ((int) +1)

Yes, definitely more convincing!

Thanks,

Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/