C++学习笔记[01]

2 数据类型

2.1 整型

在这里插入图片描述
下面展示一些 内联代码片

#include <iostream>
using namespace std;

int main()
{
    // 整型
    // 1、短整型(-32768~32767)
	short num1 = 32768;

    // 2、整型
    int num2 = 32768;

    // 3、长整型
    long num3 = 32768;

    // 4、长长整型
    long long num4 = 32768;
	std::cout << "num1=" << num1 << std::endl;
    std::cout << "num2=" << num2 << std::endl;
    std::cout << "num3=" << num3 << std::endl;
    std::cout << "num4=" << num4 << std::endl;

	system("pause");

	return 0;
}

在这里插入图片描述

2.2 sizeof关键字

#include <iostream>
using namespace std;

int main()
{
    // 整型: short(2) int(4) long(4) long long (8)
    // 可以利用sizeof求出数据变量占用的内存大小
    // 语法:sizeof(数据类型/变量)
    // 1、短整型(-32768~32767)
	short num1 = 10;
    cout << "sizeof short=" << sizeof(short) << endl;
    cout << "sizeof num1=" << sizeof(num1) << endl;


    // // 2、整型
    int num2 = 32768;
    cout << "sizeof num2=" << sizeof(num2) << endl;

    // // 3、长整型
    long num3 = 32768;
    cout << "sizeof num3=" << sizeof(num3) << endl;

    // // 4、长长整型
    long long num4 = 32768;
    cout << "sizeof num4=" << sizeof(num4) << endl;

	// system("pause");

	return 0;
}

在这里插入图片描述

2.3 实型(浮点型)

在这里插入图片描述

2.4 字符型

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{
    char ch = 'a';
    cout << ch << endl;

    // ch = "abcde"; //错误,不可以用双引号
    // ch = "adcde"; //错误,单引号内只能引用一个字符

    // 字符型变量对应ASCII编码
	cout << (int)ch << endl;
    ch = 97; //可以直接用ASCII给字符型变量赋值
    cout << ch << endl;

	// system("pause");

	return 0;
}

在这里插入图片描述

2.5 转义字符

在这里插入图片描述

2.6 字符串型

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;
# include <string> //用C++风格字符串时候,要包含这个头文件

int main()
{
   // 1、C风格字符串
   // 注意事项 char 字符串名 []
   // 注意事项2 等号后面 要用双引号 包含起来字符串
   char str[] = "hello world";
   cout << str << endl;

   // 2、C++风格字符串
   // 包含一个头文件 # include <string>
   string str2 = "hello world";
   cout << str2 << endl;

   // system("pause");

   return 0;
}

在这里插入图片描述

2.7 布尔类型 bool

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{
   // 1、创建bool数据类型
   bool flag = true; //true代表真
   cout << flag << endl;

   flag = false; //false代表真
   cout << flag << endl;

   // 本质上 1代表着真 0代表假

   // 2、查看bool类型所占内存空间
   cout << "bool类型所占内存空间=" << sizeof(flag) << endl;

   // system("pause");

   return 0;
}

在这里插入图片描述

2.8 数据的输入

在这里插入图片描述

#include <iostream>
using namespace std;
#include <string>

int main()
{
   // 1、整型
   int a = 0;
   cout << "请给子整型变量a赋值" << endl;
   cin >> a;
   cout << "整型变量a=" << a << endl;

   // 2、浮点型
   float f = 0;
   cout << "请给子浮点型变量f赋值" << endl;
   cin >> f;
   cout << "浮点型变量f=" << f << endl;

   // 3、字符型
   char ch = 'a';
   cout << "请给子字符型变量ch赋值" << endl;
   cin >> ch;
   cout << "字符型变量ch=" << ch << endl;

   // 4、字符串型
   string str = "hello";
   cout << "请给字符串型变量str赋值" << endl;
   cin >> str;
   cout << "字符型变量ch=" << str << endl;

   // 5、布尔型
   bool flag = false;
   cout << "请给布尔型变量flag赋值" << endl;
   cin >> flag; // bool类型 只要是非0的值都代表真
   cout << "布尔型变量flag=" << flag << endl;

   // system("pause");

   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值