|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: Compiler warning (level 2) C4146"
|
3 | 3 | title: "Compiler warning (level 2) C4146"
|
4 |
| -ms.date: 08/30/2022 |
| 4 | +ms.date: 04/22/2025 |
5 | 5 | f1_keywords: ["C4146"]
|
6 | 6 | helpviewer_keywords: ["C4146"]
|
7 | 7 | ---
|
8 | 8 | # Compiler warning (level 2) C4146
|
9 | 9 |
|
10 | 10 | > unary minus operator applied to unsigned type, result still unsigned
|
11 | 11 |
|
12 |
| -Unsigned types can hold only non-negative values, so unary minus (negation) usually doesn't make sense when applied to an unsigned type. Both the operand and the result are non-negative. |
| 12 | +Unsigned types only hold non-negative values. So unary minus (negation) usually doesn't make sense when applied to an unsigned type. Both the operand and the result are non-negative. |
13 | 13 |
|
14 | 14 | ## Remarks
|
15 | 15 |
|
16 |
| -When you express a negative integer literal, the **`-`** in front of the value is parsed as a [unary negation](../../cpp/unary-plus-and-negation-operators-plus-and.md) operator. The compiler applies the operator after it parses the numeric value. If the numeric value fits in the range of an unsigned integer type, but not the corresponding signed integer type, the compiler interprets the value as unsigned. An unsigned value is unchanged by the unary negation operator. |
| 16 | +When you express a negative integer literal, the **`-`** in front of the value is parsed as a [unary negation](../../cpp/unary-plus-and-negation-operators-plus-and.md) operator. The compiler applies the operator after it parses the numeric value. If the numeric value fits in the range of an unsigned integer type, but not the corresponding signed integer type, the compiler interprets the value as unsigned. |
17 | 17 |
|
18 |
| -This warning often occurs when you try to express the minimum **`int`** value, -2147483648, or the minimum **`long long`** value, -9223372036854775808. These values can't be written as -2147483648 or -9223372036854775808ll, respectively. The reason is because the compiler processes the expression in two stages: first, it parses the numeric value, then it applies the negation operator. For example, when the compiler parses -2147483648, it follows these steps: |
| 18 | +This warning often occurs when you try to express the minimum **`int`** value, -2147483648, or the minimum **`long long`** value, -9223372036854775808. These values can't be written as -2147483648 or -9223372036854775808ll. The reason is because the compiler processes the expression in two stages: first, it parses the numeric value, then it applies the negation operator. For example, when the compiler parses -2147483648, it follows these steps: |
19 | 19 |
|
20 | 20 | 1. The number 2147483648 is evaluated. Because it's greater than the maximum **`int`** value of 2147483647, but still fits in an **`unsigned int`**, the type of 2147483648 is **`unsigned int`**.
|
21 |
| - |
22 | 21 | 1. Unary minus is applied to the unsigned value, with an unsigned result, which also happens to be 2147483648.
|
23 | 22 |
|
24 | 23 | The unsigned type of the result can cause unexpected behavior. If the result is used in a comparison, then an unsigned comparison might be used, for example, when the other operand is an **`int`**.
|
25 | 24 |
|
26 |
| -You can avoid C4146 by using `INT_MIN` or `LLONG_MIN` from *`<limits.h>`* or the C++ equivalent, *`<climits>`*. These values have signed types. |
| 25 | +You can avoid **C4146** by using `INT_MIN` or `LLONG_MIN` from *`<limits.h>`* or the C++ equivalent, *`<climits>`*. These values have signed types. |
27 | 26 |
|
28 | 27 | The [`/sdl` (Enable Additional Security Checks)](../../build/reference/sdl-enable-additional-security-checks.md) compiler option elevates this warning to an error.
|
29 | 28 |
|
|
0 commit comments