Home > Blockchain >  conversion from 'long' to 'int' and warnings
conversion from 'long' to 'int' and warnings

Time:01-03

Why there is a warning for c) but not a warning for b)?

I'm assigning in both cases a long value to an int.

   // INT32_MAX = 2147483647

   // a:
   int32_t a = 2147483647;

   // b:
   // no warning
   int32_t b = 1L;

   // c:
   // warning: implicit conversion from
   //'long' to 'int'
   int32_t c = 2147483648;

2147483648 doesn't fit into an int (but 1L does). Ist this the explanation for this behaviour? Do these warnings depend on current values? I was expecting two warnings and not just one.

EDIT: compiler: clang version 13.0.0

I still have problems to understand that. Ok, 1L fits into an integer of type int.

So there are no warnings for the following line of code:

    // no warning
   int b = 1L;

But why do I get warnings for almost the same code? Because I initialize it with a variable value? But it still fits into this variable.

   // warning
   long temp = 1L;
   int a = temp;

There are no warnings after changing temp to a const value:

   // no warning
   const long temp = 1L;
   int a = temp;

CodePudding user response:

Values of expressions of type int and long are numbers (in mathematical sense). This is not spelled out explicitly in the standard, but can be inferred from the language that talks about ranges of types, for example:

6.2.5/8 8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

The value of the expression 1L (of type long) is the number 1. The value of the expression 2147483648 (of some type which in your implementation is not int) is the number 2147483648. The former belongs to the range of values of the type int, so there is no warning. The latter doesn't, so there is a warning: the value does not fit.

long temp = 1L;
int a = temp;

temp is not a constant. Its value can, in principle, be anything that fits in a long. Anything that fits in a long does not necessarily fit in an int, so there is a different warning: the value might not fit. This actually never happens in this program, but the compiler is not obliged to see that. Such things are impossible to prove in general, so your compiler doesn't try even in this simple case.

const long temp = 1L;
int a = temp;

temp is a constant. Its value is 1, and it cannot be anything other than 1. Your compiler is able to easily prove it, so it does.

Note, not all compilers do any or all of the above. There is no obligation in the standard to emit warnings.

CodePudding user response:

When long is converted to an int32_t or an int, the source value might not fit in the destination, depending on circumstances.

In int32_t b = 1L;, the compiler sees that 1L has the value one and fits in an int32_t, so it does not warn you.

In int32_t c = 2147483648;, the value does not fit in an int32_t (in this C implementation), so the compiler warns you.

  •  Tags:  
  • c
  • Related