what is typeof((c) + 1) in C

后端 未结 7 2084
梦谈多话
梦谈多话 2020-12-23 14:09

I came across an expression in C like

typeof((c) + 1) _tmp = c;

What exactly does this mean?

Thanks for the reply.

Just on

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 14:39

    In addition to the other answer, the + here is quite subtle. It allows for c to be either an expression or a type.

    • If it is an expression then, as said, c is promoted to int (at least) and the type of the whole expression has at least integer rank of int.
    • If it is a type expression the parenthesis surrounding c make it a cast of the value +1. So then the resulting type is just c.

    For both kinds of acrobatic it is important that c is of arithmetic type and it is also to note that this trick here might loose the signedness of c. So this use of the typeof extension is not so useful as it might look like. In most cases using uintmax_t or intmax_t would be sufficient.

提交回复
热议问题