news 2026/4/18 9:43:57

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/4/18 9:19:52

毕设分享 基于单片机的太阳追光系统(源码+硬件+论文)

文章目录 0 前言1 课题介绍光线追踪的原理系统架构 2 硬件设计3 核心软件设计4 实现效果5 最后 0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升&#xff0c;传统的毕设题目缺少创新和亮点&#xff0c;往往达不到毕业答辩的要求&#xff0c;这两年不断…

作者头像 李华
网站建设 2026/4/18 8:02:15

Flutter 入门实战:构建一个简单的天气应用

标题&#xff1a;Flutter 入门实战&#xff1a;构建一个简单的天气应用 引言 随着移动应用开发需求的不断增长&#xff0c;跨平台框架成为开发者关注的焦点。Flutter 作为 Google 推出的 UI 工具包&#xff0c;凭借高性能、高保真和“一次编写&#xff0c;多端运行”的优势&a…

作者头像 李华
网站建设 2026/4/18 7:42:55

Java运算中的类型转换

Java运算中的类型转换分为两类&#xff1a;隐式转换&#xff08;自动类型提升&#xff09;和强制类型转换。隐式类型转换主要由两条规则&#xff1a;取值范围小的和取值范围大的进行运算&#xff0c;小的会先提升为大的&#xff0c;再进行运算&#xff1b;byte、short、char三种…

作者头像 李华
网站建设 2026/4/18 8:06:36

想搞永磁同步电机控制又不想烧板子?联合仿真了解一下。最近拿Maxwell和Simplorer折腾了一套SVPWM控制方案,实测电流环响应速度比外卖小哥抢单还快

Maxwell和Simplorer联合仿真——永磁同步电机SVPWM控制 本仿真用AnsysEM实现永磁同步电机&#xff08;PMSM&#xff09;的仿真模拟&#xff0c;控制方式采用空间矢量控制&#xff0c;闭环方式采用电流环速度环双闭环控制。 文件中包含一个仿真文件&#xff0c;以及仿真搭建的简…

作者头像 李华
网站建设 2026/4/18 2:18:41

无线电能传输仿真模型:S-S拓扑结构及主电路参数设计

无线电能传输仿真模型&#xff0c;电路采用S-S拓扑结构。 闭环输出电压400v&#xff0c;输出效果良好。 采用的是移相控制。 另有主电路的参数设计过程。深夜两点盯着示波器屏幕&#xff0c;突然发现谐振电流的波形开始优雅地跳起华尔兹——这是我在调试S-S型无线电能传输系统时…

作者头像 李华