Does Unary + operator do type conversions?

后端 未结 4 648
眼角桃花
眼角桃花 2021-01-04 04:28

Till now I was believing that there is no use of unary + operator.

But then I came across with following example:

char ch;
short sh;
in         


        
4条回答
  •  甜味超标
    2021-01-04 05:06

    When smaller types are involved in an expression with larger types (for example, char is smaller than short which mostly is smaller than int which may be smaller than long), the involved types are promoted to the larger tyoes.

    So yes, when you use the unary + operator, you get an int, because int is the natural integer type in C.

    Regarding the double type, the natural floating point type for C is double, which is why there's no promotion on values or variables that already are of type double.

    The sizeof operator have nothing to do with this.

提交回复
热议问题