class="tags" href="/C.html" title=
c >
c ontent_views"
c lass="tags" href="/C.html" title=
c >
c lass="markdown_views prism-tomorrow-night">
class="tags" href="/C.html" title=c >c ap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-bloc lass="tags" href="/C.html" title=c >c k" style="-webkit-tap-highlight-c lass="tags" href="/C.html" title=c >c olor: rgba(0, 0, 0, 0);">
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/c lass="tags" href="/C.html" title=c >c b7f73c lass="tags" href="/C.html" title=c >c da1a14130840e356c lass="tags" href="/C.html" title=c >c b0b6f9e4.png#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" width="100" />
<
c lass="tags" href="/C.html" title=
c >
c enter>
博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳]
c lass="tags" href="/C.html" title=
c >
c enter>
<
c lass="tags" href="/C.html" title=
c >
c enter>
本文专栏: C++
c lass="tags" href="/C.html" title=
c >
c enter>
class="tags" href="/C.html" title=c >c ="https://img-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" width="1000" height="100" />
💯前言
<c lass="tags" href="/C.html" title=c >c ode>C++c lass="tags" href="/C.html" title=c >c ode> 是一门功能强大且灵活的编程语言c lass="tags" href="/C.html" title=c >c ;能够处理从简单到复杂的各种数据。理解 <c lass="tags" href="/C.html" title=c >c ode>C++c lass="tags" href="/C.html" title=c >c ode> 中的数据类型是编写高效和鲁棒代码的基石。本文将深入探讨 <c lass="tags" href="/C.html" title=c >c ode>C++c lass="tags" href="/C.html" title=c >c ode> 中各种数据类型的特性、内存占用、应用场景以及实现方式 c lass="tags" href="/C.html" title=c >c ;力求为读者提供系统性的理解。 C++ 中的数据类型可大致分为基本数据类型 和派生数据类型 。 基本数据类型 包括字符型 、整型 、浮点型 和布尔型 c lass="tags" href="/C.html" title=c >c ;这些类型是 C++ 编程中的基本单元c lass="tags" href="/C.html" title=c >c ;奠定了程序数据结构和逻辑运算的基础 。 接下来c lass="tags" href="/C.html" title=c >c ;我们将详细分析每一种数据类型的定义、特性、应用及其在程序设计中的具体实现方式。
C++ 参考手册 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/f0db7c lass="tags" href="/C.html" title=c >c 348331440fa266b3c lass="tags" href="/C.html" title=c >c 24f558694.png#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" />
c lass="tags" href="/C.html" title=c >c har_51">💯字符型(c lass="tags" href="/C.html" title=c >c har)
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 使用场景 :字符型用于存储单个字符c lass="tags" href="/C.html" title=c >c ;例如 ASCII 字符集中的字符。通常占用 1 字节 c lass="tags" href="/C.html" title=c >c ;它的存储范围可以涵盖简单字符c lass="tags" href="/C.html" title=c >c ;如字母、数字和特殊符号。字符型通常用于表示标识符、符号或者单个字母。
c lass="tags" href="/C.html" title=c >c har 是 C++ 中的基本数据类型c lass="tags" href="/C.html" title=c >c ;专门用于表示字符。存储范围 :通常为 -128 到 127(有符号)c lass="tags" href="/C.html" title=c >c ;或 0 到 255(无符号)。内存占用 :1 字节。
在 C++ 中c lass="tags" href="/C.html" title=c >c ;字符以 ASCII 编码 的形式存储c lass="tags" href="/C.html" title=c >c ;每个字符都有一个相应的 ASCII 值。例如c lass="tags" href="/C.html" title=c >c ;大写字母 ‘A’ 的 ASCII 值是 65c lass="tags" href="/C.html" title=c >c ;而小写字母 ‘a’ 的 ASCII 值是 97。ASCII 编码是一种标准化的字符编码方案c lass="tags" href="/C.html" title=c >c ;便于在不同的计算机系统之间交换信息。
1.1 ASCII 码表
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 下列表格包含有全部 128 个 ASCII 十进制 (dec lass="tags" href="/C.html" title=c >c )、八进制 (oc lass="tags" href="/C.html" title=c >c t)、十六进制 (hex) 及字符 (c lass="tags" href="/C.html" title=c >c h) 编码。
dec lass="tags" href="/C.html" title=c >c (十进制) oc lass="tags" href="/C.html" title=c >c t (八进制) hex (十六进制) c lass="tags" href="/C.html" title=c >c h (字符)0 000 00 NUL (空) 1 001 01 SOH (标题开始) 2 002 02 STX (正文开始) 3 003 03 ETX (正文结束) 4 004 04 EOT (传送结束) 5 005 05 ENQ (询问) 6 006 06 ACK (确认) 7 007 07 BEL (响铃) 8 010 08 BS (退格) 9 011 09 HT (横向制表) 10 012 0A LF (换行) 11 013 0B VT (纵向制表) 12 014 0C FF (换页) 13 015 0D CR (回车) 14 016 0E SO (移出) 15 017 0F SI (移入) 16 020 10 DLE (退出数据链) 17 021 11 DC1 (设备控制1) 18 022 12 DC2 (设备控制2) 19 023 13 DC3 (设备控制3) 20 024 14 DC4 (设备控制4) 21 025 15 NAK (反确认) 22 026 16 SYN (同步空闲) 23 027 17 ETB (传输块结束) 24 030 18 CAN (取消) 25 031 19 EM (媒介结束) 26 032 1A SUB (替换) 27 033 1B ESC (退出) 28 034 1C FS (文件分隔符) 29 035 1D GS (组分隔符) 30 036 1E RS (记录分隔符) 31 037 1F US (单元分隔符) 32 040 20 (空格) 33 041 21 ! 34 042 22 " 35 043 23 # 36 044 24 $ 37 045 25 % 38 046 26 & 39 047 27 ’ 40 050 28 ( 41 051 29 ) 42 052 2A * 43 053 2B + 44 054 2C , 45 055 2D - 46 056 2E . 47 057 2F / 48 060 30 0 49 061 31 1 50 062 32 2 51 063 33 3 52 064 34 4 53 065 35 5 54 066 36 6 55 067 37 7 56 070 38 8 57 071 39 9 58 072 3A : 59 073 3B ; 60 074 3C < 61 075 3D = 62 076 3E > 63 077 3F ? 64 100 40 @ 65 101 41 A 66 102 42 B 67 103 43 C 68 104 44 D 69 105 45 E 70 106 46 F 71 107 47 G 72 110 48 H 73 111 49 I 74 112 4A J 75 113 4B K 76 114 4C L 77 115 4D M 78 116 4E N 79 117 4F O 80 120 50 P 81 121 51 Q 82 122 52 R 83 123 53 S 84 124 54 T 85 125 55 U 86 126 56 V 87 127 57 W 88 130 58 X 89 131 59 Y 90 132 5A Z 91 133 5B [ 92 134 5C \ 93 135 5D ] 94 136 5E ^ 95 137 5F _ 96 140 60 ` 97 141 61 a 98 142 62 b 99 143 63 c lass="tags" href="/C.html" title=c >c 100 144 64 d 101 145 65 e 102 146 66 f 103 147 67 g 104 150 68 h 105 151 69 i 106 152 6A j 107 153 6B k 108 154 6C l 109 155 6D m 110 156 6E n 111 157 6F o 112 160 70 p 113 161 71 q 114 162 72 r 115 163 73 s 116 164 74 t 117 165 75 u 118 166 76 v 119 167 77 w 120 170 78 x 121 171 79 y 122 172 7A z 123 173 7B { 124 174 7C 125 175 7D } 126 176 7E ~ 127 177 7F DEL (删除)
此表适用于基础编程学习与字符编码参考。
常见的 ASCII 码应用 :
字符 <c lass="tags" href="/C.html" title=c >c ode>A-Zc lass="tags" href="/C.html" title=c >c ode> 的 ASCII 码值为 65-90 。 字符 <c lass="tags" href="/C.html" title=c >c ode>a-zc lass="tags" href="/C.html" title=c >c ode> 的 ASCII 码值为 97-122 。 数字字符 <c lass="tags" href="/C.html" title=c >c ode>0-9c lass="tags" href="/C.html" title=c >c ode> 的 ASCII 码值为 48-57 。 换行符 <c lass="tags" href="/C.html" title=c >c ode> c lass="tags" href="/C.html" title=c >c ode> 的 ASCII 码值为 10 。
在 C++ 程序中c lass="tags" href="/C.html" title=c >c ;可以使用 ASCII 码来实现字符与整数的转换c lass="tags" href="/C.html" title=c >c ;从而简化某些数据操作。例如c lass="tags" href="/C.html" title=c >c ;可以将字符转换为其对应的 ASCII 值c lass="tags" href="/C.html" title=c >c ;或者根据整数值生成相应的字符。
示例代码 :
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">class="tags" href="/C.html" title=c >c lass="token mac lass="tags" href="/C.html" title=c >c ro property">class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive-hash"># class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive keyword">inc lass="tags" href="/C.html" title=c >c lude class="tags" href="/C.html" title=c >c lass="token string"><iostream> class="tags" href="/C.html" title=c >c lass="token keyword">int class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">main class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c har c lass="tags" href="/C.html" title=c >c 1 class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c har">'A' class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token keyword">int asc lass="tags" href="/C.html" title=c >c iiValue class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token generic lass="tags" href="/C.html" title=c >c -func lass="tags" href="/C.html" title=c >c tion">class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">static lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c ast class="tags" href="/C.html" title=c >c lass="token generic lass="tags" href="/C.html" title=c >c c lass="tags" href="/C.html" title=c >c lass-name">class="tags" href="/C.html" title=c >c lass="token operator">< class="tags" href="/C.html" title=c >c lass="token keyword">int class="tags" href="/C.html" title=c >c lass="token operator">> class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( c lass="tags" href="/C.html" title=c >c 1class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"字符 A 的 ASCII 值是: " class="tags" href="/C.html" title=c >c lass="token operator"><< asc lass="tags" href="/C.html" title=c >c iiValue class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c har c lass="tags" href="/C.html" title=c >c 2 class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">66 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c omment">// 直接使用 ASCII 值表示字符 stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"ASCII 值 66 对应的字符是: " class="tags" href="/C.html" title=c >c lass="token operator"><< c lass="tags" href="/C.html" title=c >c 2 class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token keyword">return class="tags" href="/C.html" title=c >c lass="token number">0 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">}
c lass="tags" href="/C.html" title=c >c ode>
输出 :
<c lass="tags" href="/C.html" title=c >c ode>字符 A 的 ASCII 值是: 65
ASCII 值 66 对应的字符是: B
c lass="tags" href="/C.html" title=c >c ode>
在这个例子中c lass="tags" href="/C.html" title=c >c ;我们将字符 <c lass="tags" href="/C.html" title=c >c ode>'A'c lass="tags" href="/C.html" title=c >c ode> 转换为它的 ASCII 值c lass="tags" href="/C.html" title=c >c ;并直接使用 ASCII 值 <c lass="tags" href="/C.html" title=c >c ode>66c lass="tags" href="/C.html" title=c >c ode> 来表示字符 <c lass="tags" href="/C.html" title=c >c ode>'B'c lass="tags" href="/C.html" title=c >c ode>。
字符型在程序中的灵活性使得它在文本处理和符号运算中非常有用。例如c lass="tags" href="/C.html" title=c >c ;我们可以使用字符的 ASCII 值关系将所有小写字母转换为大写字母。以下是一个例子:
示例代码 :
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">class="tags" href="/C.html" title=c >c lass="token mac lass="tags" href="/C.html" title=c >c ro property">class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive-hash"># class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive keyword">inc lass="tags" href="/C.html" title=c >c lude class="tags" href="/C.html" title=c >c lass="token string"><iostream> class="tags" href="/C.html" title=c >c lass="token keyword">int class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">main class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token keyword">for class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c har c lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c har">'a' class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; c lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token operator"><= class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c har">'z' class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token operator">++ c lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c har upperCase class="tags" href="/C.html" title=c >c lass="token operator">= c lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token operator">- class="tags" href="/C.html" title=c >c lass="token number">32 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c omment">// ASCII 中大写字母与小写字母之间的差值为 32 stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< c lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">" -> " class="tags" href="/C.html" title=c >c lass="token operator"><< upperCase class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">} class="tags" href="/C.html" title=c >c lass="token keyword">return class="tags" href="/C.html" title=c >c lass="token number">0 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">}
c lass="tags" href="/C.html" title=c >c ode>
输出 :
<c lass="tags" href="/C.html" title=c >c ode>a -> A
b -> B
...
z -> Z
c lass="tags" href="/C.html" title=c >c ode>
这种方式利用了 ASCII 值之间的数学关系c lass="tags" href="/C.html" title=c >c ;有效地实现了字符的转换。此类技术在文本处理、字符比较等场景中非常有用。
💯整型(int)
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 整型用于存储整数c lass="tags" href="/C.html" title=c >c ;包括正数、负数和零。C++ 提供了多种整型c lass="tags" href="/C.html" title=c >c ;以适应不同数据范围和内存占用的需求。
2.1 整型的分类
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
short :短整型c lass="tags" href="/C.html" title=c >c ;占用 2 字节c lass="tags" href="/C.html" title=c >c ;存储范围为 -32,768 到 32,767 。适用于较小范围的整数。int :标准整型c lass="tags" href="/C.html" title=c >c ;占用 4 字节c lass="tags" href="/C.html" title=c >c ;存储范围为 -2,147,483,648 到 2,147,483,647 。适用于大多数日常应用。long :长整型c lass="tags" href="/C.html" title=c >c ;在 32 位系统中通常与 int 相同c lass="tags" href="/C.html" title=c >c ;占用 4 字节;在 64 位系统中通常占用 8 字节。适合需要更大存储空间的场景。long long :更长的整型c lass="tags" href="/C.html" title=c >c ;占用 8 字节c lass="tags" href="/C.html" title=c >c ;存储范围为 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 。适用于存储非常大的整数。
每种整型在不同平台和编译器上的实现可能有所不同c lass="tags" href="/C.html" title=c >c ;因此为了确保跨平台代码的兼容性c lass="tags" href="/C.html" title=c >c ;通常需要使用 <c lass="tags" href="/C.html" title=c >c ode><c lass="tags" href="/C.html" title=c >c stdint>c lass="tags" href="/C.html" title=c >c ode> 提供的固定宽度整数类型c lass="tags" href="/C.html" title=c >c ;例如 <c lass="tags" href="/C.html" title=c >c ode>int32_tc lass="tags" href="/C.html" title=c >c ode>、<c lass="tags" href="/C.html" title=c >c ode>int64_tc lass="tags" href="/C.html" title=c >c ode>。
2.2 有符号和无符号整型
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
有符号类型 (默认):可以存储正数和负数。无符号类型 :只能存储非负数c lass="tags" href="/C.html" title=c >c ;使用 <c lass="tags" href="/C.html" title=c >c ode>unsignedc lass="tags" href="/C.html" title=c >c ode> 关键字定义c lass="tags" href="/C.html" title=c >c ;例如 <c lass="tags" href="/C.html" title=c >c ode>unsigned intc lass="tags" href="/C.html" title=c >c ode>。
无符号整型特别适合用于确保数值始终为非负数的场景c lass="tags" href="/C.html" title=c >c ;例如循环计数器、数组索引等。
示例代码 :
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">class="tags" href="/C.html" title=c >c lass="token keyword">short s class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">32000 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token keyword">int i class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">1000000 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token keyword">long l class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">1000000000 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token keyword">long class="tags" href="/C.html" title=c >c lass="token keyword">long ll class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">9000000000000000000 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token keyword">unsigned class="tags" href="/C.html" title=c >c lass="token keyword">int ui class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">4000000000 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"short: " class="tags" href="/C.html" title=c >c lass="token operator"><< s class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"int: " class="tags" href="/C.html" title=c >c lass="token operator"><< i class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"long: " class="tags" href="/C.html" title=c >c lass="token operator"><< l class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"long long: " class="tags" href="/C.html" title=c >c lass="token operator"><< ll class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"unsigned int: " class="tags" href="/C.html" title=c >c lass="token operator"><< ui class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
在上述代码中c lass="tags" href="/C.html" title=c >c ;可以看到整型类型的多样性。无符号整型 <c lass="tags" href="/C.html" title=c >c ode>unsigned intc lass="tags" href="/C.html" title=c >c ode> 可以表示的范围从 0 到 4,294,967,295c lass="tags" href="/C.html" title=c >c ;比有符号类型多出一倍的数值范围。
2.3 跨平台差异
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 不同系统和编译器对整型的实现可能存在差异c lass="tags" href="/C.html" title=c >c ;尤其是 long 类型。在 32 位系统中c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>longc lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>intc lass="tags" href="/C.html" title=c >c ode> 的存储范围相同c lass="tags" href="/C.html" title=c >c ;而在 64 位系统中c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>longc lass="tags" href="/C.html" title=c >c ode> 通常占用 8 字节。
可以使用 <c lass="tags" href="/C.html" title=c >c ode>sizeofc lass="tags" href="/C.html" title=c >c ode> 关键字查看数据类型的实际字节数:
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"Size of int: " class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token keyword">sizeof class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token keyword">int class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">" bytes" class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
这种方式可以帮助开发者了解在不同平台上数据类型的存储情况c lass="tags" href="/C.html" title=c >c ;以确保程序的正确性和兼容性。
2.4 整型数据类型比较表
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
数据类型 占用字节数(一般情况) 有符号范围 无符号范围 short 2 -32,768 到 32,767 0 到 65,535 int 4 -2,147,483,648 到 2,147,483,647 0 到 4,294,967,295 long 4 或 8 -2,147,483,648 到 2,147,483,647(32 位) 0 到 18,446,744,073,709,551,615(64 位) long long 8 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 0 到 18,446,744,073,709,551,615
整型的数据类型选择需要根据程序的实际需求c lass="tags" href="/C.html" title=c >c ;合理权衡内存占用和数据范围。例如c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>shortc lass="tags" href="/C.html" title=c >c ode> 适合用于占用内存少的小范围整数c lass="tags" href="/C.html" title=c >c ;而 <c lass="tags" href="/C.html" title=c >c ode>long longc lass="tags" href="/C.html" title=c >c ode> 适合用于需要非常大数值范围的场景。
💯浮点型(float, double, long double)
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 浮点型用于表示带小数部分的数值c lass="tags" href="/C.html" title=c >c ;例如科学计算、物理测量等。C++ 提供了三种浮点型c lass="tags" href="/C.html" title=c >c ;以满足不同精度和内存需求。
3.1 浮点型的分类
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
float :单精度浮点型c lass="tags" href="/C.html" title=c >c ;占用 4 字节 c lass="tags" href="/C.html" title=c >c ;有效位数为 6-7 位 。适用于对内存要求较高但对精度要求不太高的场景。double :双精度浮点型c lass="tags" href="/C.html" title=c >c ;占用 8 字节 c lass="tags" href="/C.html" title=c >c ;有效位数为 15-16 位 。适用于需要较高精度的计算。long double :扩展精度浮点型c lass="tags" href="/C.html" title=c >c ;占用 10 或 16 字节 (依系统而定)c lass="tags" href="/C.html" title=c >c ;有效位数为 18-19 位 。适用于需要极高精度的场景。
浮点型在科学计算、物理仿真、金融数据分析等领域有着广泛的应用。浮点数在计算机中是通过科学计数法的方式表示的c lass="tags" href="/C.html" title=c >c ;因此浮点数的表示范围非常大c lass="tags" href="/C.html" title=c >c ;但在计算精度上可能会存在一定的误差。
3.2 在 C/C++ 中小数的书写形式
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 在 C/C++ 中c lass="tags" href="/C.html" title=c >c ;小数可以用多种形式来表示c lass="tags" href="/C.html" title=c >c ;以适应科学计数法的需求。
3.14 :编译器会默认识别为 <c lass="tags" href="/C.html" title=c >c ode>doublec lass="tags" href="/C.html" title=c >c ode> 类型。3.14f :明确声明为 <c lass="tags" href="/C.html" title=c >c ode>floatc lass="tags" href="/C.html" title=c >c ode> 类型。1e5 :科学计数法c lass="tags" href="/C.html" title=c >c ;表示 1.0 × 10^5 。1e5 + 10 :表示 1 × 100000 + 10 = 100010 。1.23e5 + 10 :表示 1.23 × 100000 + 10 = 123010 。
示例代码 :
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">class="tags" href="/C.html" title=c >c lass="token keyword">float a class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">3.14f class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c omment">// 必须加上后缀 fc lass="tags" href="/C.html" title=c >c ;否则 3.14 会被视为 double 类型
class="tags" href="/C.html" title=c >c lass="token keyword">double b class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">1e5 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c omment">// 使用科学计数法
stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"Float: " class="tags" href="/C.html" title=c >c lass="token operator"><< a class="tags" href="/C.html" title=c >c lass="token operator"><< "
Doubleclass="tags" href="/C.html" title=c >c lass="token operator">: " class="tags" href="/C.html" title=c >c lass="token operator"><< b class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
3.3 为什么需要后缀 <c lass="tags" href="/C.html" title=c >c ode>fc lass="tags" href="/C.html" title=c >c ode>?
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 在 C++ 中c lass="tags" href="/C.html" title=c >c ;默认情况下c lass="tags" href="/C.html" title=c >c ;带小数的常量被认为是 double 类型。如果要定义 float 类型的变量c lass="tags" href="/C.html" title=c >c ;必须在数值后添加 <c lass="tags" href="/C.html" title=c >c ode>fc lass="tags" href="/C.html" title=c >c ode> 或 <c lass="tags" href="/C.html" title=c >c ode>Fc lass="tags" href="/C.html" title=c >c ode> 后缀c lass="tags" href="/C.html" title=c >c ;否则会发生隐式类型转换c lass="tags" href="/C.html" title=c >c ;这可能导致性能下降或精度损失。
示例代码 :
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">class="tags" href="/C.html" title=c >c lass="token keyword">float a class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">5.5f class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c omment">// 必须加上后缀 fc lass="tags" href="/C.html" title=c >c ;否则 5.5 会被视为 double 类型
stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c omment">// 输出 5.5
c lass="tags" href="/C.html" title=c >c ode>
这种做法在对内存和性能有较高要求的场景中显得尤为重要。
3.4 浮点型数据类型比较表
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
数据类型 默认后缀 占用字节数 有效位数(精度) 范围 float f 4 6-7 位 ±3.4×10^-38 到 ±3.4×10^38 double 无 8 15-16 位 ±1.7×10^-308 到 ±1.7×10^308 long double 无 10 或 16 18-19 位 ±3.4×10^-4932 到 ±3.4×10^4932
在需要高精度计算的场景中c lass="tags" href="/C.html" title=c >c ;通常使用 double 或 long double 类型c lass="tags" href="/C.html" title=c >c ;而 float 则常用于对内存要求更高的应用中。
💯布尔型(bool)
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 布尔型用于表示逻辑上的真与假。它只有两个可能的取值:true (真)和 false (假)。布尔型在逻辑判断、条件控制和状态标记中有广泛应用。
存储范围 :<c lass="tags" href="/C.html" title=c >c ode>truec lass="tags" href="/C.html" title=c >c ode> 或 <c lass="tags" href="/C.html" title=c >c ode>falsec lass="tags" href="/C.html" title=c >c ode>。内存占用 :通常为 1 字节 c lass="tags" href="/C.html" title=c >c ;但编译器可能对其进行优化。
示例代码 :
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c pp">class="tags" href="/C.html" title=c >c lass="token keyword">bool isStudent class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token boolean">true class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token keyword">if class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( isStudentclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: c lass="tags" href="/C.html" title=c >c out class="tags" href="/C.html" title=c >c lass="token operator"><< class="tags" href="/C.html" title=c >c lass="token string">"This person is a student." class="tags" href="/C.html" title=c >c lass="token operator"><< stdclass="tags" href="/C.html" title=c >c lass="token double-c lass="tags" href="/C.html" title=c >c olon punc lass="tags" href="/C.html" title=c >c tuation">:: endlclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">}
c lass="tags" href="/C.html" title=c >c ode>
布尔型因其简单和高效而在条件判断和逻辑控制中非常常见。
💯总结与应用场景
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> C++ 中的不同数据类型各有特点c lass="tags" href="/C.html" title=c >c ;适用于不同的编程需求。通过合理选择数据类型c lass="tags" href="/C.html" title=c >c ;可以优化程序的内存利用和计算性能。
字符型 用于存储单个字符c lass="tags" href="/C.html" title=c >c ;适合用于标识符和符号表示。整型 提供了多种选择c lass="tags" href="/C.html" title=c >c ;从短整型到长整型c lass="tags" href="/C.html" title=c >c ;用于存储不同范围的整数。浮点型 适用于科学计算c lass="tags" href="/C.html" title=c >c ;提供了从单精度到扩展精度的多种精度选择。布尔型 适合用于逻辑控制和条件判断。
在嵌入式系统中c lass="tags" href="/C.html" title=c >c ;资源有限c lass="tags" href="/C.html" title=c >c ;合理选择数据类型尤为重要。而在科学计算领域c lass="tags" href="/C.html" title=c >c ;精度的选择又是核心考量。通过对数据类型的充分理解c lass="tags" href="/C.html" title=c >c ;开发者可以编写出更加高效、稳定的程序。
五、总结
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/c lass="tags" href="/C.html" title=c >c 676f4660a8a4b338b878d4b137e425b.png" alt="在这里插入图片描述" /> 理解 C++ 中的简单数据类型是编写高效 、健壮程序 的基础。 不同的数据类型具有不同的<c lass="tags" href="/C.html" title=c >c ode>存储范围c lass="tags" href="/C.html" title=c >c ode>、<c lass="tags" href="/C.html" title=c >c ode>内存占用c lass="tags" href="/C.html" title=c >c ode>和<c lass="tags" href="/C.html" title=c >c ode>适用场景c lass="tags" href="/C.html" title=c >c ode>。 在编写代码时c lass="tags" href="/C.html" title=c >c ;根据实际需求选择合适的数据类型 c lass="tags" href="/C.html" title=c >c ;既可以提高程序的运行效率c lass="tags" href="/C.html" title=c >c ;又能有效利用系统资源。 通过对 C++ 数据类型的深入理解c lass="tags" href="/C.html" title=c >c ;开发者能够更好地<c lass="tags" href="/C.html" title=c >c ode>控制内存c lass="tags" href="/C.html" title=c >c ode>、<c lass="tags" href="/C.html" title=c >c ode>优化性能c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;并为程序添加强大的功能 。 不论是字符型 、整型 、浮点型 、布尔型 还是<c lass="tags" href="/C.html" title=c >c ode>常量类型c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;它们各自的优势和适用场景使得程序开发更为灵活和高效 。
class="tags" href="/C.html" title=c >c ="https://img-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" width="1000" height="100" />