Skip to content

Repo sync for protected CLA branch #4333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 45 additions & 45 deletions docs/c-language/conversions-from-signed-integral-types.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
description: "Learn more about: Conversions from signed integral types"
title: "Conversions from signed integral types"
ms.date: "10/02/2019"
ms.date: 12/06/2022
helpviewer_keywords: ["integral conversions, from signed", "integers, converting", "conversions [C++], integral", "data type conversion [C++], signed and unsigned integers", "type conversion [C++], signed and unsigned integers"]
ms.assetid: 5eea32f8-8b14-413d-acac-c063b3d118d7
---
# Conversions from signed integral types

When a signed integer is converted to an integer or a floating-point type, if the original value is representable in the result type, the value is unchanged.
When a signed integer is converted to an integer or a floating-point type, the value is unchanged if it's representable in the result type.

When a signed integer is converted to an integer of greater size, the value is sign-extended. When converted to an integer of smaller size, the high-order bits are truncated. The result is interpreted using the result type, as shown in this example:

Expand All @@ -19,7 +19,7 @@ u = i;
printf_s( "%hu\n", u ); // Prints 65533
```

When converting a signed integer to a floating-point type, if the original value isn't representable exactly in the result type, the result is the next higher or lower representable value.
When the compiler converts a signed integer to a floating-point type, if the original value isn't representable exactly in the result type, the result is the next higher or lower representable value.

For information about the sizes of integral and floating-point types, see [Storage of basic types](../c-language/storage-of-basic-types.md).

Expand All @@ -33,48 +33,48 @@ In the Microsoft compiler, **`int`** and **`long`** are distinct but equivalent

## Table of conversions from signed integral types

|From|To|Method|
|----------|--------|------------|
|**`char`**<sup>1</sup>|**`short`**|Sign-extend|
|**`char`**|**`long`**|Sign-extend|
|**`char`**|**`long long`**|Sign-extend|
|**`char`**|**`unsigned char`**|Preserve pattern; high-order bit loses function as sign bit|
|**`char`**|**`unsigned short`**|Sign-extend to **`short`**; convert **`short`** to **`unsigned short`**|
|**`char`**|**`unsigned long`**|Sign-extend to **`long`**; convert **`long`** to **`unsigned long`**|
|**`char`**|**`unsigned long long`**|Sign-extend to **`long long`**; convert **`long long`** to **`unsigned long long`**|
|**`char`**|**`float`**|Sign-extend to **`long`**; convert **`long`** to **`float`**|
|**`char`**|**`double`**|Sign-extend to **`long`**; convert **`long`** to **`double`**|
|**`char`**|**`long double`**|Sign-extend to **`long`**; convert **`long`** to **`double`**|
|**`short`**|**`char`**|Preserve low-order byte|
|**`short`**|**`long`**|Sign-extend|
|**`short`**|**`long long`**|Sign-extend|
|**`short`**|**`unsigned char`**|Preserve low-order byte|
|**`short`**|**`unsigned short`**|Preserve bit pattern; high-order bit loses function as sign bit|
|**`short`**|**`unsigned long`**|Sign-extend to **`long`**; convert **`long`** to **`unsigned long`**|
|**`short`**|**`unsigned long long`**|Sign-extend to **`long long`**; convert **`long long`** to **`unsigned long long`**|
|**`short`**|**`float`**|Sign-extend to **`long`**; convert **`long`** to **`float`**|
|**`short`**|**`double`**|Sign-extend to **`long`**; convert **`long`** to **`double`**|
|**`short`**|**`long double`**|Sign-extend to **`long`**; convert **`long`** to **`double`**|
|**`long`**|**`char`**|Preserve low-order byte|
|**`long`**|**`short`**|Preserve low-order word|
|**`long`**|**`long long`**|Sign-extend|
|**`long`**|**`unsigned char`**|Preserve low-order byte|
|**`long`**|**`unsigned short`**|Preserve low-order word|
|**`long`**|**`unsigned long`**|Preserve bit pattern; high-order bit loses function as sign bit|
|**`long`**|**`unsigned long long`**|Sign-extend to **`long long`**; convert **`long long`** to **`unsigned long long`**|
|**`long`**|**`float`**|Represent as **`float`**. If **`long`** can't be represented exactly, some precision is lost.|
|**`long`**|**`double`**|Represent as **`double`**. If **`long`** can't be represented exactly as a **`double`**, some precision is lost.|
|**`long`**|**`long double`**|Represent as **`double`**. If **`long`** can't be represented exactly as a **`double`**, some precision is lost.|
|**`long long`**|**`char`**|Preserve low-order byte|
|**`long long`**|**`short`**|Preserve low-order word|
|**`long long`**|**`long`**|Preserve low-order dword|
|**`long long`**|**`unsigned char`**|Preserve low-order byte|
|**`long long`**|**`unsigned short`**|Preserve low-order word|
|**`long long`**|**`unsigned long`**|Preserve low-order dword|
|**`long long`**|**`unsigned long long`**|Preserve bit pattern; high-order bit loses function as sign bit|
|**`long long`**|**`float`**|Represent as **`float`**. If **`long long`** can't be represented exactly, some precision is lost.|
|**`long long`**|**`double`**|Represent as **`double`**. If **`long long`** can't be represented exactly as a **`double`**, some precision is lost.|
|**`long long`**|**`long double`**|Represent as **`double`**. If **`long long`** can't be represented exactly as a **`double`**, some precision is lost.|
| From | To | Method |
|---|---|---|
| **`char`**<sup>1</sup> | **`short`** | Sign-extend |
| **`char`** | **`long`** | Sign-extend |
| **`char`** | **`long long`** | Sign-extend |
| **`char`** | **`unsigned char`** | Preserve pattern; high-order bit loses function as sign bit |
| **`char`** | **`unsigned short`** | Sign-extend to **`short`**; convert **`short`** to **`unsigned short`** |
| **`char`** | **`unsigned long`** | Sign-extend to **`long`**; convert **`long`** to **`unsigned long`** |
| **`char`** | **`unsigned long long`** | Sign-extend to **`long long`**; convert **`long long`** to **`unsigned long long`** |
| **`char`** | **`float`** | Represent exactly as **`float`** |
| **`char`** | **`double`** | Represent exactly as **`double`** |
| **`char`** | **`long double`** | Represent exactly as **`long double`** |
| **`short`** | **`char`** | Preserve low-order byte |
| **`short`** | **`long`** | Sign-extend |
| **`short`** | **`long long`** | Sign-extend |
| **`short`** | **`unsigned char`** | Preserve low-order byte |
| **`short`** | **`unsigned short`** | Preserve bit pattern; high-order bit loses function as sign bit |
| **`short`** | **`unsigned long`** | Sign-extend to **`long`**; convert **`long`** to **`unsigned long`** |
| **`short`** | **`unsigned long long`** | Sign-extend to **`long long`**; convert **`long long`** to **`unsigned long long`** |
| **`short`** | **`float`** | Represent exactly as **`float`** |
| **`short`** | **`double`** | Represent exactly as **`double`** |
| **`short`** | **`long double`** | Represent exactly as **`long double`** |
| **`long`** | **`char`** | Preserve low-order byte |
| **`long`** | **`short`** | Preserve low-order word |
| **`long`** | **`long long`** | Sign-extend |
| **`long`** | **`unsigned char`** | Preserve low-order byte |
| **`long`** | **`unsigned short`** | Preserve low-order word |
| **`long`** | **`unsigned long`** | Preserve bit pattern; high-order bit loses function as sign bit |
| **`long`** | **`unsigned long long`** | Sign-extend to **`long long`**; convert **`long long`** to **`unsigned long long`** |
| **`long`** | **`float`** | Represent as **`float`**. If **`long`** can't be represented exactly, some precision is lost. |
| **`long`** | **`double`** | Represent exactly as **`double`** |
| **`long`** | **`long double`** | Represent exactly as **`long double`** |
| **`long long`** | **`char`** | Preserve low-order byte |
| **`long long`** | **`short`** | Preserve low-order word |
| **`long long`** | **`long`** | Preserve low-order dword |
| **`long long`** | **`unsigned char`** | Preserve low-order byte |
| **`long long`** | **`unsigned short`** | Preserve low-order word |
| **`long long`** | **`unsigned long`** | Preserve low-order dword |
| **`long long`** | **`unsigned long long`** | Preserve bit pattern; high-order bit loses function as sign bit |
| **`long long`** | **`float`** | Represent as **`float`**. If **`long long`** can't be represented exactly, some precision is lost. |
| **`long long`** | **`double`** | Represent as **`double`**. If **`long long`** can't be represented exactly as a **`double`**, some precision is lost. |
| **`long long`** | **`long double`** | Represent as **`double`**. If **`long long`** can't be represented exactly as a **`double`**, some precision is lost. |

<sup>1</sup> All **`char`** entries assume that the **`char`** type is signed by default.

Expand Down
90 changes: 45 additions & 45 deletions docs/c-language/conversions-from-unsigned-integral-types.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
description: "Learn more about: Conversions from unsigned integral types"
title: "Conversions from unsigned integral types"
ms.date: "10/02/2019"
ms.date: 12/06/2022
helpviewer_keywords: ["integers, converting", "type casts, involving integers", "data type conversion [C++], signed and unsigned integers", "type conversion [C++], signed and unsigned integers", "integral conversions, from unsigned"]
ms.assetid: 60fb7e10-bff9-4a13-8a48-e19f25a36a02
---
# Conversions from unsigned integral types

When an unsigned integer is converted to an integer or floating-point type, if the original value is representable in the result type the value is unchanged.

When converting an unsigned integer to an integer of greater size, the value is zero-extended. When converting to an integer of smaller size, the high-order bits are truncated. The result is interpreted using the result type, as shown in this example.
When the compiler converts an unsigned integer to an integer of greater size, the value is zero-extended. When converted to an integer of smaller size, the high-order bits are truncated. The result is interpreted using the result type, as shown in this example:

```C
unsigned k = 65533;
Expand All @@ -19,7 +19,7 @@ j = k;
printf_s( "%hd\n", j ); // Prints -3
```

When converting an unsigned integer to a floating-point type, if the original value can't be represented exactly in the result type, the result is the next higher or lower representable value.
When the compiler converts an unsigned integer to a floating-point type, if the original value isn't representable exactly in the result type, the result is the next higher or lower representable value.

See [Storage of basic types](../c-language/storage-of-basic-types.md) for information about the sizes of integral and floating-point types.

Expand All @@ -33,48 +33,48 @@ The following table summarizes conversions from unsigned integral types.

## Table of conversions from unsigned integral types

|From|To|Method|
|----------|--------|------------|
|**`unsigned char`**|**`char`**|Preserve bit pattern; high-order bit becomes sign bit|
|**`unsigned char`**|**`short`**|Zero-extend|
|**`unsigned char`**|**`long`**|Zero-extend|
|**`unsigned char`**|**`long long`**|Zero-extend|
|**`unsigned char`**|**`unsigned short`**|Zero-extend|
|**`unsigned char`**|**`unsigned long`**|Zero-extend|
|**`unsigned char`**|**`unsigned long long`**|Zero-extend|
|**`unsigned char`**|**`float`**|Convert to **`long`**; convert **`long`** to **`float`**|
|**`unsigned char`**|**`double`**|Convert to **`long`**; convert **`long`** to **`double`**|
|**`unsigned char`**|**`long double`**|Convert to **`long`**; convert **`long`** to **`double`**|
|**`unsigned short`**|**`char`**|Preserve low-order byte|
|**`unsigned short`**|**`short`**|Preserve bit pattern; high-order bit becomes sign bit|
|**`unsigned short`**|**`long`**|Zero-extend|
|**`unsigned short`**|**`long long`**|Zero-extend|
|**`unsigned short`**|**`unsigned char`**|Preserve low-order byte|
|**`unsigned short`**|**`unsigned long`**|Zero-extend|
|**`unsigned short`**|**`unsigned long long`**|Zero-extend|
|**`unsigned short`**|**`float`**|Convert to **`long`**; convert **`long`** to **`float`**|
|**`unsigned short`**|**`double`**|Convert to **`long`**; convert **`long`** to **`double`**|
|**`unsigned short`**|**`long double`**|Convert to **`long`**; convert **`long`** to **`double`**|
|**`unsigned long`**|**`char`**|Preserve low-order byte|
|**`unsigned long`**|**`short`**|Preserve low-order word|
|**`unsigned long`**|**`long`**|Preserve bit pattern; high-order bit becomes sign bit|
|**`unsigned long`**|**`long long`**|Zero-extend|
|**`unsigned long`**|**`unsigned char`**|Preserve low-order byte|
|**`unsigned long`**|**`unsigned short`**|Preserve low-order word|
|**`unsigned long`**|**`unsigned long long`**|Zero-extend|
|**`unsigned long`**|**`float`**|Convert to **`long`**; convert **`long`** to **`float`**|
|**`unsigned long`**|**`double`**|Convert directly to **`double`**|
|**`unsigned long`**|**`long double`**|Convert to **`long`**; convert **`long`** to **`double`**|
|**`unsigned long long`**|**`char`**|Preserve low-order byte|
|**`unsigned long long`**|**`short`**|Preserve low-order word|
|**`unsigned long long`**|**`long`**|Preserve low-order dword|
|**`unsigned long long`**|**`long long`**|Preserve bit pattern; high-order bit becomes sign bit|
|**`unsigned long long`**|**`unsigned char`**|Preserve low-order byte|
|**`unsigned long long`**|**`unsigned short`**|Preserve low-order word|
|**`unsigned long long`**|**`unsigned long`**|Preserve low-order dword|
|**`unsigned long long`**|**`float`**|Convert to **`long`**; convert **`long`** to **`float`**|
|**`unsigned long long`**|**`double`**|Convert directly to **`double`**|
|**`unsigned long long`**|**`long double`**|Convert to **`long`**; convert **`long`** to **`double`**|
| From | To | Method |
|---|---|---|
| **`unsigned char`** | **`char`** | Preserve bit pattern; high-order bit becomes sign bit |
| **`unsigned char`** | **`short`** | Zero-extend |
| **`unsigned char`** | **`long`** | Zero-extend |
| **`unsigned char`** | **`long long`** | Zero-extend |
| **`unsigned char`** | **`unsigned short`** | Zero-extend |
| **`unsigned char`** | **`unsigned long`** | Zero-extend |
| **`unsigned char`** | **`unsigned long long`** | Zero-extend |
| **`unsigned char`** | **`float`** | Convert exactly to **`float`** |
| **`unsigned char`** | **`double`** | Convert exactly to **`double`** |
| **`unsigned char`** | **`long double`** | Convert exactly to **`long double`** |
| **`unsigned short`** | **`char`** | Preserve low-order byte |
| **`unsigned short`** | **`short`** | Preserve bit pattern; high-order bit becomes sign bit |
| **`unsigned short`** | **`long`** | Zero-extend |
| **`unsigned short`** | **`long long`** | Zero-extend |
| **`unsigned short`** | **`unsigned char`** | Preserve low-order byte |
| **`unsigned short`** | **`unsigned long`** | Zero-extend |
| **`unsigned short`** | **`unsigned long long`** | Zero-extend |
| **`unsigned short`** | **`float`** | Convert exactly to **`float`** |
| **`unsigned short`** | **`double`** | Convert exactly to **`double`** |
| **`unsigned short`** | **`long double`** | Convert exactly to **`long double`** |
| **`unsigned long`** | **`char`** | Preserve low-order byte |
| **`unsigned long`** | **`short`** | Preserve low-order word |
| **`unsigned long`** | **`long`** | Preserve bit pattern; high-order bit becomes sign bit |
| **`unsigned long`** | **`long long`** | Zero-extend |
| **`unsigned long`** | **`unsigned char`** | Preserve low-order byte |
| **`unsigned long`** | **`unsigned short`** | Preserve low-order word |
| **`unsigned long`** | **`unsigned long long`** | Zero-extend |
| **`unsigned long`** | **`float`** | Convert to nearest representable **`float`** |
| **`unsigned long`** | **`double`** | Convert exactly to **`double`** |
| **`unsigned long`** | **`long double`** | Convert exactly to **`long double`** |
| **`unsigned long long`** | **`char`** | Preserve low-order byte |
| **`unsigned long long`** | **`short`** | Preserve low-order word |
| **`unsigned long long`** | **`long`** | Preserve low-order dword |
| **`unsigned long long`** | **`long long`** | Preserve bit pattern; high-order bit becomes sign bit |
| **`unsigned long long`** | **`unsigned char`** | Preserve low-order byte |
| **`unsigned long long`** | **`unsigned short`** | Preserve low-order word |
| **`unsigned long long`** | **`unsigned long`** | Preserve low-order dword |
| **`unsigned long long`** | **`float`** | Convert to nearest representable **`float`** |
| **`unsigned long long`** | **`double`** | Convert to nearest representable **`double`** |
| **`unsigned long long`** | **`long double`** | Convert to nearest representable **`long double`** |

## See also

Expand Down