news 2026/2/7 6:34:01

C++ 解释有符号整数和无符号整数修饰符之间的差别

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++ 解释有符号整数和无符号整数修饰符之间的差别

C++ 允许在char、int 和 double数据类型前放置修饰符。

修饰符是用于改变变量类型的行为的关键字,它更能满足各种情境的需求。

下面列出了数据类型修饰符:

  • signed:表示变量可以存储负数。对于整型变量来说,signed 可以省略,因为整型变量默认为有符号类型。

  • unsigned:表示变量不能存储负数。对于整型变量来说,unsigned 可以将变量范围扩大一倍。

  • short:表示变量的范围比 int 更小。short int 可以缩写为 short。

  • long:表示变量的范围比 int 更大。long int 可以缩写为 long。

  • long long:表示变量的范围比 long 更大。C++11 中新增的数据类型修饰符。

  • float:表示单精度浮点数。

  • double:表示双精度浮点数。

  • bool:表示布尔类型,只有 true 和 false 两个值。

  • char:表示字符类型。

  • wchar_t:表示宽字符类型,可以存储 Unicode 字符。

修饰符signed、unsigned、long 和 short可应用于整型,signedunsigned可应用于字符型,long可应用于双精度型。

这些修饰符也可以组合使用,修饰符signedunsigned也可以作为longshort修饰符的前缀。例如:unsigned long int

C++ 允许使用速记符号来声明无符号短整数无符号长整数。您可以不写 int,只写单词unsigned、shortlongint是隐含的。例如,下面的两个语句都声明了无符号整型变量。

signed int num1 = -10; // 定义有符号整型变量 num1,初始值为 -10 unsigned int num2 = 20; // 定义无符号整型变量 num2,初始值为 20 short int num1 = 10; // 定义短整型变量 num1,初始值为 10 long int num2 = 100000; // 定义长整型变量 num2,初始值为 100000 long long int num1 = 10000000000; // 定义长长整型变量 num1,初始值为 10000000000 float num1 = 3.14f; // 定义单精度浮点数变量 num1,初始值为 3.14 double num2 = 2.71828; // 定义双精度浮点数变量 num2,初始值为 2.71828 bool flag = true; // 定义布尔类型变量 flag,初始值为 true char ch1 = 'a'; // 定义字符类型变量 ch1,初始值为 'a' wchar_t ch2 = L'你'; // 定义宽字符类型变量 ch2,初始值为 '你'

为了理解 C++ 解释有符号整数和无符号整数修饰符之间的差别,我们来运行一下下面这个短程序:

实例

#include <iostream> using namespace std; /* * 这个程序演示了有符号整数和无符号整数之间的差别 */ int main() { short int i; // 有符号短整数 short unsigned int j; // 无符号短整数 j = 50000; i = j;

https://avg.163.com/topic/detail/7991186
https://avg.163.com/topic/detail/7995684
https://avg.163.com/topic/detail/7995711
https://avg.163.com/topic/detail/7995748
https://avg.163.com/topic/detail/7995786
https://avg.163.com/topic/detail/7991202
https://avg.163.com/topic/detail/7995683
https://avg.163.com/topic/detail/7995712
https://avg.163.com/topic/detail/7995750
https://avg.163.com/topic/detail/7991196
https://avg.163.com/topic/detail/7995679
https://avg.163.com/topic/detail/7995709
https://avg.163.com/topic/detail/7995743
https://avg.163.com/topic/detail/7991166
https://avg.163.com/topic/detail/7995685
https://avg.163.com/topic/detail/7995716
https://avg.163.com/topic/detail/7995784
https://avg.163.com/topic/detail/7991096
https://avg.163.com/topic/detail/7995681
https://avg.163.com/topic/detail/7995720
https://avg.163.com/topic/detail/7995747
https://avg.163.com/topic/detail/7995776
https://avg.163.com/topic/detail/7991124
https://avg.163.com/topic/detail/7991139
https://avg.163.com/topic/detail/7995689
https://avg.163.com/topic/detail/7995723
https://avg.163.com/topic/detail/7995763
https://avg.163.com/topic/detail/7991065
https://avg.163.com/topic/detail/7995678
https://avg.163.com/topic/detail/7995718
https://avg.163.com/topic/detail/7995752
https://avg.163.com/topic/detail/7995782
https://avg.163.com/topic/detail/7991012
https://avg.163.com/topic/detail/7995687
https://avg.163.com/topic/detail/7995721
https://avg.163.com/topic/detail/7995751
https://avg.163.com/topic/detail/7995785
https://avg.163.com/topic/detail/7991037
https://avg.163.com/topic/detail/7995672
https://avg.163.com/topic/detail/7995715
https://avg.163.com/topic/detail/7995744
https://avg.163.com/topic/detail/7995777
https://avg.163.com/topic/detail/7991001
https://avg.163.com/topic/detail/7995688
https://avg.163.com/topic/detail/7995767
https://avg.163.com/topic/detail/7995793
https://avg.163.com/topic/detail/7990752
https://avg.163.com/topic/detail/7995674
https://avg.163.com/topic/detail/7990895
https://avg.163.com/topic/detail/7995686
https://avg.163.com/topic/detail/7995745
https://avg.163.com/topic/detail/7995713
https://avg.163.com/topic/detail/7995781
https://avg.163.com/topic/detail/7995742
https://avg.163.com/topic/detail/7995775
https://avg.163.com/topic/detail/7990941
https://avg.163.com/topic/detail/7995671
https://avg.163.com/topic/detail/7995707
https://avg.163.com/topic/detail/7995739
https://avg.163.com/topic/detail/7995773
https://avg.163.com/topic/detail/7990836
https://avg.163.com/topic/detail/7995676
https://avg.163.com/topic/detail/7995710
https://avg.163.com/topic/detail/7995741
https://avg.163.com/topic/detail/7995778
https://avg.163.com/topic/detail/7990704
https://avg.163.com/topic/detail/7995669
https://avg.163.com/topic/detail/7995708
https://avg.163.com/topic/detail/7995740
https://avg.163.com/topic/detail/7995774
https://avg.163.com/topic/detail/7990658
https://avg.163.com/topic/detail/7995680
https://avg.163.com/topic/detail/7995705
https://avg.163.com/topic/detail/7995772
https://avg.163.com/topic/detail/7990676
https://avg.163.com/topic/detail/7995677
https://avg.163.com/topic/detail/7995704
https://avg.163.com/topic/detail/7995736
https://avg.163.com/topic/detail/7995771
https://avg.163.com/topic/detail/7990599
https://avg.163.com/topic/detail/7995717
https://avg.163.com/topic/detail/7995749
https://avg.163.com/topic/detail/7995783
https://avg.163.com/topic/detail/7990453
https://avg.163.com/topic/detail/7995706
https://avg.163.com/topic/detail/7995738
https://avg.163.com/topic/detail/7995770
https://avg.163.com/topic/detail/7990389
https://avg.163.com/topic/detail/7995673
https://avg.163.com/topic/detail/7995703
https://avg.163.com/topic/detail/7995735
https://avg.163.com/topic/detail/7995769
https://avg.163.com/topic/detail/7990360
https://avg.163.com/topic/detail/7995668
https://avg.163.com/topic/detail/7995702
https://avg.163.com/topic/detail/7995734
https://avg.163.com/topic/detail/7995768
https://avg.163.com/topic/detail/7990321
https://avg.163.com/topic/detail/7995527
https://avg.163.com/topic/detail/7995557
https://avg.163.com/topic/detail/7995594
https://avg.163.com/topic/detail/7995623
https://avg.163.com/topic/detail/7990316
https://avg.163.com/topic/detail/7995525
https://avg.163.com/topic/detail/7995554
https://avg.163.com/topic/detail/7995581
https://avg.163.com/topic/detail/7995618
https://avg.163.com/topic/detail/7990310
https://avg.163.com/topic/detail/7995529
https://avg.163.com/topic/detail/7995552
https://avg.163.com/topic/detail/7995626
https://avg.163.com/topic/detail/7990312
https://avg.163.com/topic/detail/7995534
https://avg.163.com/topic/detail/7995562
https://avg.163.com/topic/detail/7995587
https://avg.163.com/topic/detail/7995612
https://avg.163.com/topic/detail/7990307
https://avg.163.com/topic/detail/7995531
https://avg.163.com/topic/detail/7995563
https://avg.163.com/topic/detail/7995596
https://avg.163.com/topic/detail/7990303
https://avg.163.com/topic/detail/7995533
https://avg.163.com/topic/detail/7995556
https://avg.163.com/topic/detail/7995583
https://avg.163.com/topic/detail/7995619
https://avg.163.com/topic/detail/7990304
https://avg.163.com/topic/detail/7995526
https://avg.163.com/topic/detail/7995565
https://avg.163.com/topic/detail/7995595
https://avg.163.com/topic/detail/7995625
https://avg.163.com/topic/detail/7990300
https://avg.163.com/topic/detail/7995521
https://avg.163.com/topic/detail/7995548
https://avg.163.com/topic/detail/7995580
https://avg.163.com/topic/detail/7995609
https://avg.163.com/topic/detail/7990279
https://avg.163.com/topic/detail/7995523
https://avg.163.com/topic/detail/7995561
https://avg.163.com/topic/detail/7995586
https://avg.163.com/topic/detail/7995620
https://avg.163.com/topic/detail/7990295
https://avg.163.com/topic/detail/7995520
https://avg.163.com/topic/detail/7995550
https://avg.163.com/topic/detail/7995582
https://avg.163.com/topic/detail/7995613
https://avg.163.com/topic/detail/7990290
https://avg.163.com/topic/detail/7995537
https://avg.163.com/topic/detail/7995567
https://avg.163.com/topic/detail/7995599
https://avg.163.com/topic/detail/7995637
https://avg.163.com/topic/detail/7990282
https://avg.163.com/topic/detail/7995519
https://avg.163.com/topic/detail/7995558
https://avg.163.com/topic/detail/7995591
https://avg.163.com/topic/detail/7995622
https://avg.163.com/topic/detail/7990220
https://avg.163.com/topic/detail/7995535
https://avg.163.com/topic/detail/7995553
https://avg.163.com/topic/detail/7995590
https://avg.163.com/topic/detail/7995624
https://avg.163.com/topic/detail/7990264
https://avg.163.com/topic/detail/7995524
https://avg.163.com/topic/detail/7995551
https://avg.163.com/topic/detail/7995585
https://avg.163.com/topic/detail/7995610
https://avg.163.com/topic/detail/7990226
https://avg.163.com/topic/detail/7995532
https://avg.163.com/topic/detail/7995560
https://avg.163.com/topic/detail/7995593
https://avg.163.com/topic/detail/7995617
https://avg.163.com/topic/detail/7990223
https://avg.163.com/topic/detail/7995564
https://avg.163.com/topic/detail/7995616
https://avg.163.com/topic/detail/7990218
https://avg.163.com/topic/detail/7995522
https://avg.163.com/topic/detail/7995549
https://avg.163.com/topic/detail/7995579
https://avg.163.com/topic/detail/7995611
https://avg.163.com/topic/detail/7990214
https://avg.163.com/topic/detail/7995530
https://avg.163.com/topic/detail/7995559
https://avg.163.com/topic/detail/7995615
https://avg.163.com/topic/detail/7990189
https://avg.163.com/topic/detail/7995528
https://avg.163.com/topic/detail/7995555
https://avg.163.com/topic/detail/7995588
https://avg.163.com/topic/detail/7995621
https://avg.163.com/topic/detail/7990168
https://avg.163.com/topic/detail/7995392
https://avg.163.com/topic/detail/7995407
https://avg.163.com/topic/detail/7995433
https://avg.163.com/topic/detail/7995460
https://avg.163.com/topic/detail/7990154
https://avg.163.com/topic/detail/7995383
https://avg.163.com/topic/detail/7995400
https://avg.163.com/topic/detail/7995444
https://avg.163.com/topic/detail/7990148
https://avg.163.com/topic/detail/7995409
https://avg.163.com/topic/detail/7995436
https://avg.163.com/topic/detail/7995459
https://avg.163.com/topic/detail/7990141
https://avg.163.com/topic/detail/7995408
https://avg.163.com/topic/detail/7995427
https://avg.163.com/topic/detail/7995455
https://avg.163.com/topic/detail/7990136
https://avg.163.com/topic/detail/7995388
https://avg.163.com/topic/detail/7995405
https://avg.163.com/topic/detail/7995426
https://avg.163.com/topic/detail/7990130
https://avg.163.com/topic/detail/7995434
https://avg.163.com/topic/detail/7995454
https://avg.163.com/topic/detail/7995385
https://avg.163.com/topic/detail/7995402
https://avg.163.com/topic/detail/7995429
https://avg.163.com/topic/detail/7995458
https://avg.163.com/topic/detail/7990028
https://avg.163.com/topic/detail/7995384
https://avg.163.com/topic/detail/7995398
https://avg.163.com/topic/detail/7995422
https://avg.163.com/topic/detail/7995453
https://avg.163.com/topic/detail/7990084
https://avg.163.com/topic/detail/7995389
https://avg.163.com/topic/detail/7995412
https://avg.163.com/topic/detail/7995440
https://avg.163.com/topic/detail/7995470
https://avg.163.com/topic/detail/7989987
https://avg.163.com/topic/detail/7995386
https://avg.163.com/topic/detail/7995399
https://avg.163.com/topic/detail/7995430
https://avg.163.com/topic/detail/7995457
https://avg.163.com/topic/detail/7989399
https://avg.163.com/topic/detail/7989968
https://avg.163.com/topic/detail/7995413
https://avg.163.com/topic/detail/7995387
https://avg.163.com/topic/detail/7995441
https://avg.163.com/topic/detail/7995406
https://avg.163.com/topic/detail/7989121
https://avg.163.com/topic/detail/7995432
https://avg.163.com/topic/detail/7995393
https://avg.163.com/topic/detail/7995461
https://avg.163.com/topic/detail/7995404
https://avg.163.com/topic/detail/7995475
https://avg.163.com/topic/detail/7995425
https://avg.163.com/topic/detail/7995448
https://avg.163.com/topic/detail/7989309
https://avg.163.com/topic/detail/7995391
https://avg.163.com/topic/detail/7995401
https://avg.163.com/topic/detail/7995423
https://avg.163.com/topic/detail/7995445
https://avg.163.com/topic/detail/7989212
https://avg.163.com/topic/detail/7995377
https://avg.163.com/topic/detail/7995419
https://avg.163.com/topic/detail/7995443
https://avg.163.com/topic/detail/7989023
https://avg.163.com/topic/detail/7989033
https://avg.163.com/topic/detail/7989053
https://avg.163.com/topic/detail/7995380
https://avg.163.com/topic/detail/7995379
https://avg.163.com/topic/detail/7995403
https://avg.163.com/topic/detail/7995397
https://avg.163.com/topic/detail/7995424
https://avg.163.com/topic/detail/7995462
https://avg.163.com/topic/detail/7995451
https://avg.163.com/topic/detail/7995284
https://avg.163.com/topic/detail/7995319
https://avg.163.com/topic/detail/7995347
https://avg.163.com/topic/detail/7995368
https://avg.163.com/topic/detail/7989030
https://avg.163.com/topic/detail/7995378
https://avg.163.com/topic/detail/7995396
https://avg.163.com/topic/detail/7995420
https://avg.163.com/topic/detail/7995446
https://avg.163.com/topic/detail/7989013
https://avg.163.com/topic/detail/7995287
https://avg.163.com/topic/detail/7995313
https://avg.163.com/topic/detail/7995340
https://avg.163.com/topic/detail/7995366
https://avg.163.com/topic/detail/7988989
https://avg.163.com/topic/detail/7995289
https://avg.163.com/topic/detail/7995314
https://avg.163.com/topic/detail/7995342
https://avg.163.com/topic/detail/7988953
https://avg.163.com/topic/detail/7995285
https://avg.163.com/topic/detail/7995309
https://avg.163.com/topic/detail/7995345
https://avg.163.com/topic/detail/7995364
https://avg.163.com/topic/detail/7988867
https://avg.163.com/topic/detail/7988918
https://avg.163.com/topic/detail/7995273

cout << i << " " << j; return 0; }

当上面的程序运行时,会输出下列结果:

-15536 50000

上述结果中,无符号短整数 50,000 的位模式被解释为有符号短整数 -15,536。

C++ 中的类型限定符

类型限定符提供了变量的额外信息,用于在定义变量或函数时改变它们的默认行为的关键字。

限定符含义
constconst定义常量,表示该变量的值不能被修改。
volatile修饰符volatile告诉该变量的值可能会被程序以外的因素改变,如硬件或其他线程。。
restrictrestrict修饰的指针是唯一一种访问它所指向的对象的方式。只有 C99 增加了新的类型限定符 restrict。
mutablemutable 用于修饰类的成员变量。被 mutable 修饰的成员变量可以被修改,即使它们所在的对象是 const 的。
static用于定义静态变量,表示该变量的作用域仅限于当前文件或当前函数内,不会被其他文件或函数访问。
register用于定义寄存器变量,表示该变量被频繁使用,可以存储在CPU的寄存器中,以提高程序的运行效率。

在 C++11 中被标记为弃用(deprecated) 在 C++17 中被正式移除。

const 实例

const int NUM = 10; // 定义常量 NUM,其值不可修改 const int* ptr = &NUM; // 定义指向常量的指针,指针所指的值不可修改 int const* ptr2 = &NUM; // 和上面一行等价

volatile 实例

volatile int num = 20; // 定义变量 num,其值可能会在未知的时间被改变

mutable 实例

class Example { public: int get_value() const { return value_; // const 关键字表示该成员函数不会修改对象中的数据成员 } void set_value(int value) const { value_ = value; // mutable 关键字允许在 const 成员函数中修改成员变量 } private: mutable int value_; };

static 实例

void example_function() { static int count = 0; // static 关键字使变量 count 存储在程序生命周期内都存在 count++; }

register 实例

void example_function(register int num) { // register 关键字建议编译器将变量 num 存储在寄存器中 // 以提高程序执行速度 // 但是实际上是否会存储在寄存器中由编译器决定 }

C++11 后 register 的变化:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/3 1:04:35

C++ 中另一种用户自定义的可用的数据类型

结构用于表示一条记录&#xff0c;假设您想要跟踪图书馆中书本的动态&#xff0c;您可能需要跟踪每本书的下列属性&#xff1a; Title &#xff1a;标题Author &#xff1a;作者Subject &#xff1a;类目Book ID &#xff1a;书的 ID 定义结构 在 C 中&#xff0c;struct 语…

作者头像 李华
网站建设 2026/2/3 1:03:38

Agentic 组织下的终极拷问:康威定律是否已失效?

引言&#xff1a;当“智能体”成为组织的新成员梅尔康威在 1968 年提出的经典洞察——康威定律&#xff0c;在软件开发领域被奉为圭臬&#xff1a;“设计系统的组织&#xff08;广义上的&#xff09;注定会产生与该组织内部沟通结构相对应的设计。”&#xff0c;典型如编译器的…

作者头像 李华
网站建设 2026/2/4 21:32:02

计算机毕业设计springboot母婴护理中心信息管理系统 基于Spring Boot的母婴护理中心信息管理平台设计与实现 Spring Boot架构下的母婴护理中心管理系统开发

计算机毕业设计springboot母婴护理中心信息管理系统587329 &#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。 随着社会经济的快速发展和人们生活水平的提高&#xff0c;母婴护理服…

作者头像 李华
网站建设 2026/2/7 6:32:44

计算机毕业设计springboot旅游自助系统 基于SpringBoot框架的旅游智能自助服务平台设计与实现 SpringBoot驱动的旅游自助服务系统开发与应用研究

计算机毕业设计springboot旅游自助系统b18499&#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。随着互联网技术的飞速发展&#xff0c;人们的生活方式发生了翻天覆地的变化&#xf…

作者头像 李华
网站建设 2026/2/6 23:53:33

律师必备!揭秘高效协作的5款顶级App!

律师异地协作&#xff1a;明律坊平台实用指南引言在法律行业中&#xff0c;异地办案已成为常态。然而&#xff0c;地域壁垒、资源不均、成本高昂等问题&#xff0c;一直是困扰律师们的难题。据统计&#xff0c;异地办案的平均成本高达2000-3000元&#xff0c;且需要2-3天的时间…

作者头像 李华
网站建设 2026/2/2 23:22:54

交换机.路由器.防火墙-技术提升【7.5】

18.5.3 配置 EtherType 访问列表 EtherType 访问列表由一条以上 ACE 构成,用于指定 EtherType。 EtherType 规则借助 16 位十六进制数值控制 EtherType 标识,与控制其他类型的通信一样。配置 EtherType 访问列表 分为两个步骤,即先通过添加 ACE 创建一个访问列表并为其指定…

作者头像 李华