|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: Compiler Warning (levels 3 and 4) C4244"
|
3 | 3 | title: "Compiler Warning (levels 3 and 4) C4244"
|
4 |
| -ms.date: "11/04/2016" |
5 |
| -ms.assetid: f116bb09-c479-4b4e-a647-fe629a1383f6 |
| 4 | +ms.date: "11/6/2023" |
6 | 5 | ---
|
7 | 6 | # Compiler Warning (levels 3 and 4) C4244
|
8 | 7 |
|
9 | 8 | 'conversion' conversion from 'type1' to 'type2', possible loss of data
|
10 | 9 |
|
11 |
| -An integer type is converted to a smaller integer type. This is a level-4 warning if *type1* is **`int`** and *type2* is smaller than **`int`**. Otherwise, it is a level 3 (assigned a value of type [__int64](../../cpp/int8-int16-int32-int64.md) to a variable of type **`unsigned int`**). A possible loss of data may have occurred. |
| 10 | +An integer type is converted to a smaller integer type. |
| 11 | +- This is a level-4 warning if *type1* is a signed or unsigned **`int`** and *type2* is a smaller, such as a signed or unsigned **`short`**. |
| 12 | +- It is a level 3 warning if a value of type [`__int64`](../../cpp/int8-int16-int32-int64.md) or **`unsigned __int64`** is assigned to a signed or unsigned **`int`**. A possible loss of data may have occurred due to a narrowing conversion, which might lead to unexpected results. |
12 | 13 |
|
13 |
| -If you get C4244, you should either change your program to use compatible types, or add some logic to your code, to ensure that the range of possible values will always be compatible with the types you are using. |
| 14 | +To fix this warning, either change your program to use compatible types, or add logic that ensures that the range of possible values is compatible with the types you are using. If the conversion is intended, use an explicit cast to silence the warning. |
14 | 15 |
|
15 |
| -C4244 can also fire at level 2; see [Compiler Warning (level 2) C4244](../../error-messages/compiler-warnings/compiler-warning-level-2-c4244.md) for more information. |
16 |
| - |
17 |
| -The conversion may have a problem due to implicit conversions. |
| 16 | +C4244 can also appear when the warning level is 2; see [Compiler Warning (level 2) C4244](../../error-messages/compiler-warnings/compiler-warning-level-2-c4244.md) for more information. |
18 | 17 |
|
19 | 18 | The following sample generates C4244:
|
20 | 19 |
|
21 | 20 | ```cpp
|
22 | 21 | // C4244_level4.cpp
|
23 | 22 | // compile with: /W4
|
24 |
| -int aa; |
25 |
| -unsigned short bb; |
| 23 | +void test(unsigned short num) {} |
| 24 | + |
26 | 25 | int main() {
|
27 |
| - int b = 0, c = 0; |
28 |
| - short a = b + c; // C4244 |
| 26 | + int int1 = 1; |
| 27 | + unsigned int uint1 = 2; |
29 | 28 |
|
30 |
| - bb += c; // C4244 |
31 |
| - bb = bb + c; // C4244 |
32 |
| - bb += (unsigned short)aa; // C4244 |
33 |
| - bb = bb + (unsigned short)aa; // OK |
| 29 | + short short1 = int1; // C4244 |
| 30 | + short short2 = (short)int1; // warning silenced - explicit cast |
| 31 | + |
| 32 | + short short3 = uint1; // C4244 |
| 33 | + unsigned short ushort = uint1; // C4244 |
| 34 | + test(uint1); // C4244 |
34 | 35 | }
|
35 | 36 | ```
|
36 | 37 |
|
37 |
| -For more information, see [Usual Arithmetic Conversions](../../c-language/usual-arithmetic-conversions.md). |
| 38 | +For more information, see [Usual Arithmetic Conversions](../../c-language/usual-arithmetic-conversions.md).\ |
| 39 | +For more information about setting the warning level in Visual Studio, see [To set the compiler options in the Visual Studio development environment](../../build/reference/compiler-option-warning-level.md#to-set-the-compiler-options-in-the-visual-studio-development-environment) |
38 | 40 |
|
39 | 41 | ```cpp
|
40 | 42 | // C4244_level3.cpp
|
41 | 43 | // compile with: /W3
|
42 | 44 | int main() {
|
43 |
| - __int64 i = 8; |
44 |
| - unsigned int ii = i; // C4244 |
| 45 | + __int64 i64 = 1; |
| 46 | + unsigned __int64 u64 = 2; |
| 47 | +
|
| 48 | + int int1 = i64; // C4244 |
| 49 | + int int3 = u64; // C4244 |
45 | 50 | }
|
46 | 51 | ```
|
47 | 52 |
|
48 |
| -Warning C4244 can occur when building code for 64-bit targets that does not generate the warning when building for 32-bit targets. For example, a difference between pointers is a 32-bit quantity on 32-bit platforms, but a 64-bit quantity on 64-bit platforms. |
| 53 | +Warning C4244 can occur when building code for 64-bit targets that does not generate the warning when building for 32-bit targets. For example, pointer arithmetic results in a 32-bit quantity on 32-bit platforms, but a 64-bit quantity on 64-bit platforms. |
49 | 54 |
|
50 | 55 | The following sample generates C4244 when compiled for 64-bit targets:
|
51 | 56 |
|
|
0 commit comments