Does implicit typecasting in C maintain the same rank for char and short variables?

72 Views Asked by At

In C programming the implicit typecasting order from Higher to lower:

is the rank of char and short same here, means there is no implicit conversion from char to short or short to char right?

char is - 1 byte, short is - 2 bytes

since the size is different for both these types, how they would be in the same rank?

1

There are 1 best solutions below

0
Andrew Henle On

The image you have provided is wrong. First, it's flat-out incorrect to rank short int with char types. Per 6.3.1.1 Boolean, characters, and integers, paragraph 1 of the (draft) C11 standard:

The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

And C numeric types have a much more complex arrangement than the simple linear "rank" of the image.

Note that the floating point types float, double, and long double are not ranked with the integer types in 6.3.1.1 quoted above. As noted in the comments on your question, when numeric types are mixed they are subject to what are called usual arithmetic conversions:

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

  • First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.
  • Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.
  • Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.62)
  • Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
    • If both operands have the same type, then no further conversion is needed.
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
    • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
    • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

Note the order there - first, types are converted to an appropriate floating point type, if applicable.

Then "Otherwise, the integer promotions are performed on both operands." That's important - variables of a type smaller than int are promoted to int (or unsigned int) before calculations are performed. The image you provided does not address that. Integer promotion is specified as:

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

Only after all that are the integer ranks applicable.

And all of that is done on a per-operator basis, by precedence. Some results might be surprising if you weren't aware of that:

int a, b;
float x;

...

float y = x + a/b;

In that case, the calculation of a/b is done using integer arithmetic, not floating-point. Only after a/b is calculated as an integer value is it converted to a float and added to x. So if a is less than b, 0.0 will be added to x.