02x数据类型8-15

admin 2025年1月15日10:52:49评论5 views字数 6170阅读20分34秒阅读模式

02x数据类型8-15

基本的内置类型

C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:

类型
关键字
布尔型
bool
字符型
char
整型
int
浮点型
float
双浮点型
double
无类型
void
宽字符型
wchar_t
类型
描述
bool
布尔类型,存储值 true 或 false,占用 1 个字节。
char
字符类型,用于存储 ASCII 字符,通常占用 1 个字节。
int
整数类型,通常用于存储普通整数,通常占用 4 个字节。
float
单精度浮点值,用于存储单精度浮点数。单精度是这样的格式,1 位符号,8 位指数,23 位小数,通常占用4个字节。
double
双精度浮点值,用于存储双精度浮点数。双精度是 1 位符号,11 位指数,52 位小数,通常占用 8 个字节。
void
表示类型的缺失。
wchar_t
宽字符类型,用于存储更大范围的字符,通常占用 2 个或 4 个字节。

下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类

型的变量所能存储的最大值和最小值。

注意:不同系统会有所差异,一字节为 8 位。

注意:默认情况下,int、short、long都是带符号的,即 signed。

注意:long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。

类型
范围
char
1 个字节
-128 到 127 或者 0 到 255
unsigned char
1 个字节
0 到 255
signed char
1 个字节
-128 到 127
int
4 个字节
-2147483648 到 2147483647
unsigned int
4 个字节
0 到 4294967295
signed int
4 个字节
-2147483648 到 2147483647
short int
2 个字节
-32768 到 32767
unsigned short int
2 个字节
0 到 65,535
signed short int
2 个字节
-32768 到 32767
long int
8 个字节
-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int
8 个字节
-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int
8 个字节
0 到 18,446,744,073,709,551,615
float
4 个字节
精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double
8 个字节
双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long long
8 个字节
双精度型占8 个字节(64位)内存空间,表示 -9,223,372,036,854,775,807 到 9,223,372,036,854,775,807 的范围
long double
16 个字节
长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t
2 或 4 个字节
1 个宽字符

sizeof 用法

#include<iostream>  #include<limits>usingnamespacestd;  intmain(){  cout << "type: tt" << "************size**************"<< endl;  cout << "bool: tt" << "所占字节数:" << sizeof(bool);  cout << "t最大值:" << (numeric_limits<bool>::max)();  cout << "tt最小值:" << (numeric_limits<bool>::min)() << endl;  cout << "char: tt" << "所占字节数:" << sizeof(char);  cout << "t最大值:" << (numeric_limits<char>::max)();  cout << "tt最小值:" << (numeric_limits<char>::min)() << endl;  cout << "signed char: t" << "所占字节数:" << sizeof(signedchar);  cout << "t最大值:" << (numeric_limits<signedchar>::max)();  cout << "tt最小值:" << (numeric_limits<signedchar>::min)() << endl;  cout << "unsigned char: t" << "所占字节数:" << sizeof(unsignedchar);  cout << "t最大值:" << (numeric_limits<unsignedchar>::max)();  cout << "tt最小值:" << (numeric_limits<unsignedchar>::min)() << endl;  cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t);  cout << "t最大值:" << (numeric_limits<wchar_t>::max)();  cout << "tt最小值:" << (numeric_limits<wchar_t>::min)() << endl;  cout << "short: tt" << "所占字节数:" << sizeof(short);  cout << "t最大值:" << (numeric_limits<short>::max)();  cout << "tt最小值:" << (numeric_limits<short>::min)() << endl;  cout << "int: tt" << "所占字节数:" << sizeof(int);  cout << "t最大值:" << (numeric_limits<int>::max)();  cout << "t最小值:" << (numeric_limits<int>::min)() << endl;  cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned);  cout << "t最大值:" << (numeric_limits<unsigned>::max)();  cout << "t最小值:" << (numeric_limits<unsigned>::min)() << endl;  cout << "long: tt" << "所占字节数:" << sizeof(long);  cout << "t最大值:" << (numeric_limits<long>::max)();  cout << "t最小值:" << (numeric_limits<long>::min)() << endl;  cout << "unsigned long: t" << "所占字节数:" << sizeof(unsignedlong);  cout << "t最大值:" << (numeric_limits<unsignedlong>::max)();  cout << "t最小值:" << (numeric_limits<unsignedlong>::min)() << endl;  cout << "double: t" << "所占字节数:" << sizeof(double);  cout << "t最大值:" << (numeric_limits<double>::max)();  cout << "t最小值:" << (numeric_limits<double>::min)() << endl;  cout << "long double: t" << "所占字节数:" << sizeof(longdouble);  cout << "t最大值:" << (numeric_limits<longdouble>::max)();  cout << "t最小值:" << (numeric_limits<longdouble>::min)() << endl;  cout << "float: tt" << "所占字节数:" << sizeof(float);  cout << "t最大值:" << (numeric_limits<float>::max)();  cout << "t最小值:" << (numeric_limits<float>::min)() << endl;  cout << "size_t: t" << "所占字节数:" << sizeof(size_t);  cout << "t最大值:" << (numeric_limits<size_t>::max)();  cout << "t最小值:" << (numeric_limits<size_t>::min)() << endl;  cout << "string: t" << "所占字节数:" << sizeof(string) << endl;  // << "t最大值:" << (numeric_limits<string>::max)() << "t最小值:" << (numeric_limits<string>::min)() << endl;  cout << "type: tt" << "************size**************"<< endl;  return0;  }
02x数据类型8-15
image-20250114110035983

typedef 声明

您可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:

typedef type newname; 

派生数据类型

数据类型
描述
示例
数组
相同类型元素的集合
int arr[5] = {1, 2, 3, 4, 5};
指针
存储变量内存地址的类型
int* ptr = &x;
引用
变量的别名
int& ref = x;
函数
函数类型,表示函数的签名
int func(int a, int b);
结构体
用户定义的数据类型,可以包含多个不同类型的成员
struct Point { int x; int y; };
用户定义的数据类型,支持封装、继承和多态
class MyClass { ... };
联合体
多个成员共享同一块内存
union Data { int i; float f; };
枚举
用户定义的整数常量集合
enum Color { RED, GREEN, BLUE };

类型别名

别名
描述
示例
typedef
为现有类型定义别名
typedef int MyInt;
using
为现有类型定义别名(C++11 引入)
using MyInt = int;

标准库类型

数据类型
描述
示例
std::string
字符串类型
std::string s = "Hello";
std::vector
动态数组
std::vector<int> v = {1, 2, 3};
std::array
固定大小数组(C++11 引入)
std::array<int, 3> a = {1, 2, 3};
std::pair
存储两个值的容器
std::pair<int, float> p(1, 2.0);
std::map
键值对容器
std::map<int, std::string> m;
std::set
唯一值集合
std::set<int> s = {1, 2, 3};

常见创建变量错误

写值时的单双引号

02x数据类型8-15
image-20250114111334428

一个字符变量想写多个字符的方法

02x数据类型8-15
image-20250114111459334
02x数据类型8-15
image-20250114111615242

转义字符

转义序列
含义
\
字符
'
' 字符
"
" 字符
?
? 字符
a
警报铃声
b
退格键
f
换页符
n
换行符
r
回车
t
水平制表符 占8个位置
v
垂直制表符
ooo
一到三位的八进制数
xhh . . .
一个或多个数字的十六进制数

t水平制表符的对齐效果

02x数据类型8-15
image-20250114113459943

v垂直制表符对齐效果

02x数据类型8-15
image-20250114113903353

字符串型

  1. C风格型字符串: char charname[] = "ABCDEF",注意是双引号
  2. C++风格字符串:string strname = "ABCDEF",注意是双引号。头文件要包含#include <string>
02x数据类型8-15
image-20250114114437784
#include<iostream>  #include<limits>#include<string>usingnamespacestd;intmain(){char ch1[] = "Hello World"// C-style stringstring str1 = "Hello C++"// C++ stringcout << ch1 << endl;cout << str1 << endl;return0;}

布尔类型

bool:表示布尔类型,只有 true(1) 和 false(0) 两个值。

02x数据类型8-15
image-20250114115219203
#include<iostream>  usingnamespacestd;intmain(){bool flag = true;cout << flag << endl flag = 0;cout << flag << endl;cout << sizeof(flag) << endl}

数据输入

从键盘获取输入: cin >> 变量

02x数据类型8-15
image-20250114170107765
#include<iostream>  usingnamespacestd;intmain(){int a = 10//int是C++中的整型cout << "a的值为" << a << endl;cout << "输入a的新值" << endl;cin >> a;cout << "a的新值为" << a << endl;float b = 10.5//float是C++中的浮点类型cout << "b的值为" << b << endl;cout << "输入b的新值" << endl;cin >> b;cout << "b的新值为" << b << endl;char c = 'a'//char是C++中的字符类型cout << "c的值为" << c << endl;cout << "输入c的新值" << endl;cin >> c;cout << "c的新值为" << c << endl;bool d = true//bool是C++中的布尔类型cout << "d的值为" << d << endl;cout << "输入d的新值" << endl;cin >> d; //只要非0都是真cout << "d的新值为" << d << endl;string e = "Hello World"//string是C++中的字符串类型cout << "e的值为" << e << endl;cout << "输入e的新值" << endl;cin >> e;cout << "e的新值为" << e << endl;return0;}
02x数据类型8-15
image-20250114170511130

原文始发于微信公众号(泷羽Sec-静安):02x数据类型8-15

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2025年1月15日10:52:49
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   02x数据类型8-15https://cn-sec.com/archives/3630817.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息