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

Re: Error in 'man operator'

From: Alejandro Colomar (man-pages) <hidden>
Date: 2021-08-28 00:17:52

Hi David,

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

Alex

-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help