1
votes

In 6.3 Conversions, the integer conversion rank for signed integer types is defined as proportional with the precision.

The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision. C11 §6.3.1.1 1

After that, it says,

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 ... etc

So, this is ambiguous. Because the precision of int can be equal either to the precision of long or equal to the precision of short, depending on the implementation of int.

The precision of int may either be that one of long or short (definition in <limits.h>). On the other hand it says that rank(int) < rank (short), even if in <limits.h> they may be the same.

Where is the ambiguity in all this stuff?

4
There's no ambiguity. That "the rank of any signed integer type shall be greater than the rank of any signed integer type with less precision" does not imply that the signed integer types with the same precision need have the same rank. - davmac
rank / range. rank is used for conversions and integer types are strictly ordered by rank, but their range (precision if you want) are weakly ordered. range of long may equals range of ints, but rank of long is greater than rank of int. - Jean-Baptiste Yunès

4 Answers

2
votes

Generally long long > long > int > short when it comes to precision. Even if that's not the case and they're all the same precision this still means that the rank is long long > long > int > short. The second passage explicitly states that.

short won't ever have a higher rank than long for example. Even if they're the same precision on the platform.

In other words, the precision of the data type can differ from platform to platform (it could be long long > long > int > short but could also be long long == long == int == short), the rank of these types is always the same, as specified by the standard : long long > long > int > short.

There is no ambiguity because the first passage:

The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision..

Only says that a type with a higher precision than the other type must have a higher rank than the other type.

Then the second passage:

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 ... etc

Is for cases where the platform has 2 different types with equal precision, in this case the standard says that the rank is always : long long > long > int > short.

Precision of a type is platform dependent (with the only exception that a type of a higher rank as specified in the standard must have equal or bigger precision than the type with a lower rank). rank of a type is specified by the standard and thus platform independent.

1
votes

There is no ambiguity.

The two passages you quote:

The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision..

and

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 ... etc

... do not contradict each other. Let us assign a numeric rank to each of long int, int and short int. Suppose the rank of long int is 2, the rank of int is 1 and the rank of short int is 0. Now, regardless of whether int has the same precision as short int or long int or somewhere in between, both the above statements are satisfied. There is no contradiction and no ambiguity.

I think you are incorrectly interpreting the first statement as saying that signed integer types with the same precision must have the same rank, however, it does not say this. It merely requires that if two signed integer types have different precision, the one with greater precision has higher rank than the other. I also suspect that by "ambiguous" you mean "self-contradictory" (which, again, it is not).

0
votes

So, this is ambiguous. Because the precision of int can be equal either to the precision of long or equal to the precision of short, depending on the implementation of int.

It may be difficult to follow, but it is not ambiguous. There is no conflict between the two provisions you quoted because nowhere do they say that the integer conversion rank of two different types with the same precision must be equal.

In practice, the precision-based requirement is most applicable to extended integer types; for the standard integer types it is redundant with the explicitly-given ordering requirement.

Where is the ambiguity in all this stuff ?

The standards committee works hard to avoid ambiguity in the specification. Although it has not been completely successful, it has done a pretty good job in that regard. There's no ambiguity involving the provisions you ask about.

0
votes

Precision

Because the precision of int can be equal either to the precision of long or equal to the precision of short, depending on the implementation of int. ... The precision of int may either be that one of long or short

This is true, but incomplete. The precision of int can be instead be unequal to both short and long. In that case, it will be between short and int.

The following is required by the C spec. Precision may or may not be the same (ties) between the types.

precision(short) <= precision(int) <= precision(long)

Rank

On the other hand it says that rank(int) < rank (short), even if in they may be the same.

When operations involve different types, the type of lower rank operand is converted to the type of the higher ranking operand. The is independent of precision. With short, int, long, there are no ties concerning rank. The difference in rank is true even if all 3 types had the same precision.

rank(short) < rank(int) < rank(long)