sizeof是C/C++中的一个操作符(operator),简单的说其作用就是返回一个对象或者类型所占的内存字节数。
一、整型、实型(浮点型)
#include <iostream> using namespace std; int main() { // 1、短整型 -32,768 ~ 32,767 short num1 = 32767; //2、整形 -2,147,483,648 ~ 2,147,483,647 int num2 = 2147483647; //3、长整型 -2,147,483,648 ~ 2,147,483,647 long num3 = 2147483647; //4、长长整形 -92233372036854775808 ~ 922333720368547758087 long long num4 = 9223372036854775807; cout << "num1 =" << num1 << endl; cout << "num2 =" << num2 << endl; cout << "num3 =" << num3 << endl; cout << "num4 =" << num4 << endl; cout << "系统定义整形的上下限" << endl; cout << INT8_MIN << endl; cout << INT8_MAX << endl; cout << INT16_MIN << endl; cout << INT16_MAX << endl; cout << INT32_MIN << endl; cout << INT32_MAX << endl; cout << INT64_MIN << endl; cout << INT64_MAX << endl; cout << "实型 浮点型" << endl; // 单精度 float f1 = 3.141f; // 双精度 double d1 = 3.1415925; // 科学记数法 float f2 = 3e2; float f3 = 3e-2; // 默认显示6位有效数字 cout << "f1 =" << f1 << endl; cout << "d1 =" << d1 << endl; cout << "f2 =" << f2 << endl; cout << "f3 =" << f3 << endl; cout << "sizeof" << endl; cout << "short类型所占的空间" << sizeof(short) << endl; cout << "int类型所占的空间" << sizeof(int) << endl; cout << "long类型所占的空间" << sizeof(long) << endl; cout << "long long类型所占的空间" << sizeof(long long) << endl; cout << "float类型所占的空间" << sizeof(float) << endl; cout << "double类型所占的空间" << sizeof(double) << endl; return 0; }
num1 =32767 num2 =2147483647 num3 =2147483647 num4 =9223372036854775807 系统定义整形的上下限 -128 127 -32768 32767 -2147483648 2147483647 -9223372036854775808 9223372036854775807 实型 浮点型 f1 =3.141 d1 =3.14159 f2 =300 f3 =0.03 sizeof short类型所占的空间2 int类型所占的空间4 long类型所占的空间4 long long类型所占的空间8 float类型所占的空间4 double类型所占的空间8
二、字符型、转义字符、字符串
#include <iostream> using namespace std; #include <string> int main() { //1、字符 只能用单引号,只能是单个字符 char ch = 'a'; cout << "ch =" << ch << endl; // char底层保持的是ASCII cout << int(ch) << endl; cout << char(97) << endl; cout << "char类型所占的空间" << sizeof(char) << endl; // 2、转义字符 // 换行符 \n cout << "hello world\n"; //反斜杠 cout << "\\" << endl; //水平制表符 cout << "aa\tbbbb" << endl; cout << "aaaa\tbbbb" << endl; cout << "aaaaa\tbbbb" << endl; cout << "aaaaaaaaaaa\tbbbb" << endl; // 3、字符串 char str[] = "hello world"; cout << str << endl; string str2 = "hello world"; cout << str2 << endl; return 0; }
ch =a 97 a char类型所占的空间1 hello world \ aa bbbb aaaa bbbb aaaaa bbbb aaaaaaaaaaa bbbb hello world hello world
三、布尔类型
#include <iostream> using namespace std; int main() { bool flag = true; cout << flag << endl; cout << false << endl; cout << "bool 类型所占的空间" << sizeof(bool) << endl; return 0; }
1 0 bool 类型所占的空间1
四、数据输入
#include <iostream> using namespace std; int main() { // 1、整型 int num = 0; cout << "请输入整型数值:" << endl; cin >> num; cout << "输入的值:" << num << endl; // 2、浮点型 float f1 = 0; cout << "请输入浮点型数值:" << endl; cin >> f1; cout << "输入的值:" << f1 << endl; // 3、字符型 char ch = 0; cout << "请输入字符型值:" << endl; cin >> ch; cout << "输入的值:" << ch << endl; // 4、字符串型 char str[] = ""; cout << "请输入字符型值:" << endl; cin >> str; cout << "输入的值:" << str << endl; // 5、布尔类型 bool flag = true; cout << "请输入布尔型值:" << endl; cin >> flag; cout << "输入的值:" << flag << endl; return 0; }
请输入整型数值: 12 输入的值:12 请输入浮点型数值: 2.13 输入的值:2.13 请输入字符型值: a 输入的值:a 请输入字符型值: sdafafasfds 输入的值:sdafafasfds 请输入布尔型值: false 输入的值:0