c++基础(二)

2.数据类型

存在的意义:给变量分配合适的内存空间; 取值范围不一样

  • short 2个字节
  • int 4个字节
  • long 8个字节(64位)
  • long long 8个字节

2.1 整型

2.2 sizeof关键字

**作用:**利用sizeof关键字统计数据类所占内存的大小

语法:sizeof(数据类型/变量)

示例:

#include <iostream>
using namespace std;

int main(){

    cout << "short: \t\t" << "所占字节数:" << sizeof(short);
    cout << "\t最大值:" << (numeric_limits<short>::max)();
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
    cout << "int: \t\t" << "所占字节数:" << sizeof(int);
    cout << "\t最大值:" << (numeric_limits<int>::max)();
    cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
    cout << "long: \t\t" << "所占字节数:" << sizeof(long);
    cout << "\t最大值:" << (numeric_limits<long>::max)();
    cout << "\t\t最小值:" << (numeric_limits<long>::min)() << endl;
    cout << "long long : \t" << "所占字节数:" << sizeof(long long );
    cout << "\t最大值:" << (numeric_limits<long long >::max)();
    cout << "\t最小值:" << (numeric_limits<long long >::min)() << endl;

    return 0;
}

运行结果:

short:          所占字节数:2   最大值:32767           最小值:-32768
int:            所占字节数:4   最大值:2147483647      最小值:-2147483648
long:           所占字节数:4   最大值:2147483647              最小值:-2147483648
long long :     所占字节数:8   最大值:9223372036854775807     最小值:-9223372036854775808

结论: short < int <= long <=long long

2.3实型(浮点型)

作用:表示小数

  • 单精度 float 占用空间4字节 7位有效数字
  • 双精度 double 占用空间8字节 15-16位有效数字

示例:

int main() {
    //默认情况下 输出一个小数,会显示出6位有效数字

    float f1 = 3.1415926f;  //  加f 否则默认是双精度

    cout << "f1 " << f1 << endl;

    double d1 = 3.1415926;

    cout << "d1" << d1 << endl;

    //统计 float double 占用的内存空间
    cout << "float的内存空间 " << sizeof(float) << endl;
    cout << "float的内存空间 " << sizeof(double) << endl;

    //科学计数法
    float f2 = 3e2; //3*10^2
    cout << "f2=  " << f2 << endl;

    float f3 = 3e-2;
    cout << "f3=  " << f3 << endl;
    
    return 0;
}

运行结果:

f1 3.14159
d13.14159
float的内存空间 4
float的内存空间 8
f2=  300
f3=  0.03

2.4 字符型

作用:字符型变量用于显示单个字符

语法char(ch)= 'a';

显示字符变量的时候,用单引号将字符括起来,不要用双引号;

单引号内只能有一个字符,不可以是字符

  • c和c++中字符型变量只占用1个字节
  • 字符型变量并不是把字符本身放在内存储存,而是将对应ASCII编码储存单元

示例:

#include <iostream>
using namespace std;

int main() {
    
    //字符型变量创建方式
    char ch = 'a';
    cout << ch << endl;

    //字符型变量所占内存大小
    cout << "char字符型变量所占的内存:" << sizeof(char)<< endl;

    //字符型变量对应的ASCII编码
    cout << int(ch) << endl;  // a-97  A-65
    
    return 0;
}

运行结果:

a
char字符型变量所占的内存:1
97

2.5 转义字符

作用:用于表示一些不能显示出来的ASCII字符

一般:\n \\ \t

转义字符含义ASCII
\a警报007
\b退格008
\n换行符010
\t水平制表符009
\\代表一个 \092
\‘一个单引号039
\"一个双引号字符034
?一个 问号063

示例:

#include <iostream>
using namespace std;

int main() {

    cout << "hello world \n";
    cout << "\\" << endl;
    cout << "aa\thello\n";

    return 0;
}

运行结果:

hello world
\
aa      hello

2.6字符类型

作用:用于表示一串字符

两种风格:

  • c语言风格 char 变量名[] = "字符串值"

注意 1 char 字符串名[] 中括号要加[ ]
注意2 等号后面要用双引号才行

  • c++风格 string 变量名 = "字符串值"

示例:

#include <iostream>
using namespace std;
#include<string>  //c++风格要使用
int main() {

    //c风格  注意 char 字符串名[]  中括号要加
        // 等号后面要用双引号才行
    char str[] = "hello world ";
    cout << str << endl;

    //c++风格  包含一个头文件
    string str2 = "hello world ";
    cout << str2 << endl;
    return 0;
}

运行结果:

hello world
hello world

2.7 布尔类型

作用:代表真假

  • true 真 (1)
  • fasle 假 (0)

bool类型占 1个字节大小

示例:

#include <iostream>
using namespace std;
int main() {

    bool flag = true;
    cout << flag << endl;
    flag = false;
    cout << flag << endl;

    return 0;
}

运行结果:

1
0

2.8 数据输入

作用:从键盘获取数据

关键字cin

语法cin >> 变量

示例:

#include<string>
#include <iostream>
using namespace std;
int main() {

    //1.整型
    int a = 0;
    cout << "请给整型赋值" << endl;
    cin >> a;
    cout << "整型变量a = " << a << endl;
   
    //浮点型
    float f = 3.14f;
     cout << "请给浮点赋值" << endl;
     cin >> f;
     cout << "浮点型变量f = " << f << endl;
     //字符型
     char ch = 'a';
     //字符串型
    string str = "hello";
    cout << "请给字符串赋值" << endl;
    cin >> str;
    cout << "字符串变量str = " << str << endl;
    //布尔类型
    bool flag = false;
    cout << "请给布尔赋值" << endl;
    cin >> flag;   //布尔类型 只要是非0的值都代表真
    cout << "布尔型变量flag = " << flag << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_水哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值