What are the general rules for comparing different data types in C?

Suppose I have the following scripts:

int i = 10; short s = 5; if (s == i){ do stuff... } else if (s < i) { do stuff... } 

When C does the comparison, does it convert the smaller data type, in this case, short to int or does it convert the data type on the right to the data type on the left? In this case, int to short?

+6
source share
4 answers

This is determined by ordinary arithmetic conversions. For simple cases, the general rule is that a type with "lesser" precision is converted to a type with "more" precision, but it becomes somewhat more complicated if you start mixing signed and unsigned .

In C99, this is described in section 6.3.1.8, which I am here for convenience:

  • First, if the corresponding real type of any operand is long double , the other operand is converted, without changing the type of the domain, to a type whose corresponding real type is long double .

  • Otherwise, if the corresponding real type of any operand is double , the other operand is converted, without changing the domain type, to a type whose corresponding real type is double .

  • Otherwise, if the corresponding real type of any operand is float , the other operand is converted, without changing the domain type, to a type whose corresponding real type is float .

  • Otherwise, whole promotions run on both operands. then the following rules apply to advanced operands:

    • If both operands are of the same type, then further conversion will not be necessary.
    • Otherwise, if both operands are of integer types or both have unsigned integer types, the operand with the type of a smaller integer conversion rank is converted to the type of the operand of a higher rank.
    • Otherwise, if the operand with an unsigned integer type has a rank greater than or equal to the ranks of the type of the other operand, then the operand with a signed integer type is converted to the operand type with an unsigned integer.
    • Otherwise, if the type of the operand with an integer type sign can represent all values, the type of the operand is an unsigned integer type, then the operand with an unsigned integer type is converted to the type of the operand with an integer type.
    • Otherwise, both operands are converted to an unsigned integer corresponding to the type of operands with a signed integer type.

I highlighted the part applicable to your specific example.

The concept of an integer conversion rank is defined in section 6.3.1.1 and basically describes what you can expect (types with less precision have a lower rank than types with more accuracy).

+8
source

From Type Conversion :

The many implicit transformations on page 44, although informally stated, exactly match what you need to remember. They are easy to remember if you notice that, according to the authors, the `lower' type is promoted to the `higher' type,'' where the order '' types

 char < short int < int < long int < float < double < long double 

This rule is easy to remember - “lower to higher”, but with regard to registered and unsigned integer types, this does not help much, they are well explained in the Oli post. But it is easy to remember and will help you in most cases.

+5
source

As a rule, C will not compare two values ​​if they are not the same type and will never explicitly convert a variable to a type with less precision. In your sample code, short rises to int , which is equivalent to writing:

 int i = 10; short s = 5; if ((int)s == i){ do stuff... } else if ((int)s < i) { do stuff... } 

This will do exactly what you expect, but the same does not apply to matched / unsigned comparisons.

+2
source

Data types are an abstraction of types. As far as the computer is concerned, there is no int or short . There is memory and there is data.

When you say int x , you tell the computer "give me enough bytes to store int", when you say short y , you say ... you guessed it.

short , as you expected, takes fewer bytes, and then int and, therefore, can (and often) contain data in neighboring bytes. When comparing data of different types, the problem is that "adjacent bits cause distorted results or not?"

Whenever you compare two different types of data, you really compare bits stored in two different places. The maximum number of individual bits stored to represent data must be the same size for comparison to work

Casting is used for this.

+1
source

Source: https://habr.com/ru/post/892405/


All Articles