Thread (9 messages) 9 messages, 3 authors, 2021-08-28

Re: Error in 'man operator'

From: David Sletten <hidden>
Date: 2021-08-28 00:20:25

Ha. Nice work.

On 8/27/21 8:17 PM, Alejandro Colomar (man-pages) wrote:
Hi David,

On 8/28/21 2:01 AM, Alejandro Colomar (man-pages) wrote:
quoted
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
Cheers,

Alex
-- 
Have all good days!
David Sletten
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help