Skip to content

Commit ee7d808

Browse files
Merge pull request #4799 from Rageking8/fix-error-in-w3-w4-c4244
Fix error in /W3 /W4 C4244
2 parents d1d56c3 + e4dd293 commit ee7d808

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

docs/error-messages/compiler-warnings/compiler-warning-levels-3-and-4-c4244.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,56 @@
11
---
22
description: "Learn more about: Compiler Warning (levels 3 and 4) C4244"
33
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"
65
---
76
# Compiler Warning (levels 3 and 4) C4244
87

98
'conversion' conversion from 'type1' to 'type2', possible loss of data
109

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

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

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

1918
The following sample generates C4244:
2019

2120
```cpp
2221
// C4244_level4.cpp
2322
// compile with: /W4
24-
int aa;
25-
unsigned short bb;
23+
void test(unsigned short num) {}
24+
2625
int main() {
27-
int b = 0, c = 0;
28-
short a = b + c; // C4244
26+
int int1 = 1;
27+
unsigned int uint1 = 2;
2928

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
3435
}
3536
```
3637
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)
3840
3941
```cpp
4042
// C4244_level3.cpp
4143
// compile with: /W3
4244
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
4550
}
4651
```
4752

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

5055
The following sample generates C4244 when compiled for 64-bit targets:
5156

0 commit comments

Comments
 (0)