Click the blue
Follow us![C++ Fundamentals 007 [Notes Version - Data Type Conversion]](https://boardor.com/wp-content/uploads/2025/11/e0ea1abb-3d86-4504-b03d-e959347abc45.gif)
C++ Fundamentals 007 – Data Type ConversionIntegerFloating PointCharacter01Automatic Conversion [Implicit Conversion]
Small box → big box, can fit automatically
It’s like pouring the contents of a small box into a big box without manual operation; the computer will complete it automatically.Characteristics: Safe, no data loss.1>>>Integer (int) to Decimal (double)
<span>int</span> is a small box for integers (e.g., <span>5</span>), <span>double</span> is a big box for decimals (e.g., <span>5.0</span>). When putting an integer into a decimal box, the computer will automatically add <span>.0</span> at the end.
![C++ Fundamentals 007 [Notes Version - Data Type Conversion]](https://boardor.com/wp-content/uploads/2025/11/13a63d5d-8652-4954-af14-ac21ba56e788.png)
2>>>Character (char) to Integer (int)
<span>char</span> holds a single character (e.g., <span>'A'</span>), but the computer actually stores characters as numbers (ASCII code). When putting a character into an integer box, it will automatically convert to the corresponding number.
![C++ Fundamentals 007 [Notes Version - Data Type Conversion]](https://boardor.com/wp-content/uploads/2025/11/15a7fb7e-c44e-46b0-8b72-29a2d24b7f7d.png)
02Forced Conversion: Big Box → Small Box, requires manual “squeezing”
It’s like stuffing the contents of a big box into a small box; you must manually tell the computer “I want to do this,” and it may “squeeze out” some content (data loss).
※ Format: (target type) data to convert
1>>>Decimal (double) to Integer (int)
The content in the decimal box (e.g., <span>3.8</span>) is larger than the integer box, and during forced conversion, the decimal part will be “cut off,” leaving only the integer.
![C++ Fundamentals 007 [Notes Version - Data Type Conversion]](https://boardor.com/wp-content/uploads/2025/11/09034d8f-c022-49a8-84da-b6d8440b4e17.png)
2>>>Integer (int) to Character (char)
If the number in the integer box is between 0-127 (ASCII code range), it can be forcibly converted to the corresponding character; exceeding this range may yield strange symbols.
![C++ Fundamentals 007 [Notes Version - Data Type Conversion]](https://boardor.com/wp-content/uploads/2025/11/4a5904b1-18e1-47e9-a3e1-850d825b6c01.png)
03Summary: When to use which conversion?
🔁 Remember the following two aspects:
1.Automatic Conversion:
-
Small box → big box (e.g.,
<span>int</span>→<span>double</span>,<span>char</span>→<span>int</span>): The computer converts automatically, use with confidence.
2.Manual Conversion:
-
Big box → small box (e.g.,
<span>double</span>→<span>int</span>,<span>int</span>→<span>char</span>): Must use<span>(target type)</span>for manual conversion, and be aware of potential data loss (e.g., converting decimal to integer will lose the decimal part).
Summary:It’s easy to put small building blocks into a big box (automatic conversion); stuffing a big toy into a small box requires effort and may damage a bit (forced conversion)~
Youth Programming StudioWeChat ID: yy18337897171
Concern for Children’s Growth
Cultivating Innovative Abilities
C++ Fundamentals 001 [Notes Version]C++ Fundamentals 002 [Notes Version]C++ Fundamentals 003 [Notes Version – Operator Summary]C++ Fundamentals 004 [Notes Version – Common Data Types]C++ Fundamentals 005 [Notes] Common Mathematical Library FunctionsC++ Fundamentals 006 [Notes Version – Three Major Structures]