A.s. Bhullar
A.s. Bhullar

Reputation: 2788

Does Unary + operator do type conversions?

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;
int i;

printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i)); // output: 1 2 4

printf("%d %d %d",sizeof(+ch),sizeof(+sh),sizeof(i)); // output: 4 4 4

Does it mean + is doing type conversion here?

Because it is behaving same as following

printf("%d %d %d",sizeof((int)ch),sizeof((int)sh),sizeof(i)); // output: 4 4 4

This forces me to think + is doing type conversion.

But then I try it on double

double f;
printf("%d %d %d", sizeof(+f), sizeof((int)f), sizeof(f)); // output: 8 4 8

This forces me to rethink about unary + operator.

So my second question is: does unary + operator has special effect in sizeof operator?

Upvotes: 17

Views: 2876

Answers (4)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158629

Unary + performs integer promotions on its operand, we can see this by going to the draft C99 standard section 6.5.3.3 Unary arithmetic operators which says (emphasis mine going forward):

The result of the unary + operator is the value of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.

and section 6.3.1 Arithmetic operands says:

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.

Note that all other types are unchanged by the integer promotions and thereofore double stays a double. This would also hold for float as well, which would not be promoted to double.

Also note that using %d for the result of sizeof is undefined behavior since the result is size_t. the proper format specifier would be %zu.

Upvotes: 23

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37954

Operator unary + triggers "initial" usual arithmeric conversions, so all integer operands, which type has lower rank than rank of int and unsigned int are promoted to int (or unsigned int if int type does not cover all values of type being promoted on that implementation).

Upvotes: 4

torek
torek

Reputation: 490068

It's not sizeof, it's the unary + itself. The operands of unary + undergo "the usual arithmetic conversions". See, e.g., this other answer involving ordinary addition.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409482

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.

Upvotes: 4

Related Questions