C++,整型,sizeof,浮点型,科学计数法,字符型,转义字符,字符串型,布尔数据类型,数据输入

目录

整型

 sizeof

浮点型 

科学计数法

字符型

转义字符

 字符串型

布尔型 

数据输入


整型

 C++规定在创建一个变量或者常量时,必须要指定相应的数据类型,否则无法给变量分配内存 

数据类型存在的意义:

给变量分配合适的内存空间。

 sizeof

#include<iostream>
using namespace std;

#define Day 7
int main() 
{
	//sizeof(数据类型/变量)
	short num1 = 10;
	cout << "short占用的内存空间为:" << sizeof(short) << endl;
	//或者cout << "short占用的内存空间为:" << sizeof(num1) << endl;
	//输出:short占用的内存空间为:2

	int num2 = 10;
	cout << "int占用的内存空间为:" << sizeof(num2) << endl;
	//输出:int占用的内存空间为:4

	long num3= 10;
	cout << "long占用的内存空间为:" << sizeof(long) << endl;

	long long num4 = 10;
	cout << "long long 占用的内存空间为:" << sizeof(long long) << endl;
	/*
	输出
	long占用的内存空间为:4
    long long 占用的内存空间为:8
	*/
	system("pause");

	return 0;

}

浮点型 

科学计数法

#include<iostream>
using namespace std;

#define Day 7
int main()
{
	float f1 = 3.14;
	//把鼠标放到3.14上,会发现显示double
	//默认情况下编辑器会把小数当作doule
	//用float接收它,会把3.14转换为单精度在使用,
	//所以通常在后面加个f
	float f2 = 3.14f;
	cout << "f2=" << f2 << endl;
	//输出:f2=3.14

	double d1 = 3.14;
	cout << "d1=" << d1 << endl;
	//输出:d1=3.14

	//默认情况下 输出一个小数,会显示6位有效数字
	double d2 = 3.1415926;
	cout << "d2=" << d2 << endl;
	//输出:d2=3.14159
	cout <<"float占用的内存空间为:" << sizeof(float) << endl;
	//输出:float占用的内存空间为:4
	cout << "double占用的内存空间为:" << sizeof(double) << endl;
	//输出:double占用的内存空间为:8


	//科学计数法
	double d3 = 3e2;//3*10^2
	cout << "d3=" << d3 << endl;
	//输出:d3=300
	double d4 = 3e-2;//3*(0.1)^2
	cout << "d4=" << d4 << endl;
	//输出:d4=0.03
	system("pause");

	return 0;

}

字符型

#include<iostream>
using namespace std;

#define Day 7
int main()
{
	//1、字符型变量的创建方式
	char ch = 'a';
	cout << ch << endl;//输出:a

	//2、字符型变量所占的内存大小
	cout << sizeof(char) << endl;//输出:1

	//3、字符型变量常见错误
	//char ch2="b";//创建字符型变量的时候,要用单引号
	//char ch2 = 'abcdef';//单引号内只能放一个字符

	//4、字符型变量对应的ASCII码
	cout << (int)ch << endl;//把字符型转换成整型
	//输出:97
	system("pause");

	return 0;

}

转义字符

#include<iostream>
using namespace std;

#define Day 7
int main()
{
	//转义字符
	// 
	// 换行符 \n
	cout << "hello word\n";

	// 反斜杠 \\

	cout<<"\\"<<endl;
      
	// 水平制表符 \t有八个空格大小,作用可以整齐的输出数据
	cout << "aa\thelloword" << endl;
	cout << "aaaa\thelloword" << endl;
	cout << "aaaaaaaa\thelloword" << endl;
	cout << "aaaaaaaaa\thelloword" << endl;
	//输出:97
	system("pause");

	return 0;

}

 字符串型

#include<iostream>
using namespace std;
#include<string>//用C++风格字符串的时候,要包含这个头文件
//推荐使用万能头文件
//#include<bits/stdc++.h>

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

	//C++风格的字符串
	string str2 = "hello world";
	cout << str2 << endl;


	system("pause");

	return 0;

}

C语言中没有字符串这种数据类型,可以通过char型数组来替代; 注意:

C语言中,字符串一定是一个char型数组,但char型数组未必是字符串。

在C语言的char型数组中,数字0(和‘\0’等价)结尾的char数组就是一个字符串,但如果char型数组没有以数字0(或‘\0’)结尾,那么就不是一个字符串,只是普通字符数组,既然是一个普通的字符数组那我们指定不能一口气输出,需要挨个输出。

在C语言中,字符串总是以'\0'作为结尾,所以'\0'也被称为字符串结束标志,或者字符串结束符。

'\0'是 ASCII 码表中的第 0 个字符,英文称为 NUL,中文称为“空字符”。该字符既不能显示,也没有控制功能,输出该字符不会有任何效果,它在C语言中唯一的作用就是作为字符串结束标志。
#include <iostream>
using namespace std;
int main() {
    char c1[] = { 'c', ' ', 'p', 'r', 'o', 'g' }; //普通字符数组

    cout << c1 << endl;; //乱码,因为没有'\0’或0结束符,不是字符串,不能一口气输出来
    //他就是普通的字符数组,那我们要挨个输出,
    for (int i = 0; i < sizeof(c1) / sizeof(c1[0]); i++)
    {
        cout << c1[i] << " ";
    }
    cout << endl;
   
}

 输出结果

#include <iostream>
using namespace std;
int main() {
    //以‘\0’(或0)结尾的字符数组是字符串

    char c2[] = { 'c', ' ', 'p', 'r', 'o', 'g', '\0' };

    cout<<c2<<endl;

    char c3[] = { 'c', ' ', 'p', 'r', 'o', 'g', 0 };
    cout << c3 << endl;

    //字符串处理以‘\0’(或0)作为结束符,后面的'h', 'l', 'l', 'e', 'o'不会输出
    char c4[] = { 'c', ' ', 'p', 'r', 'o', 'g', '\0', 'h', 'l', 'l', 'e', 'o', '\0' };
    cout << c4 << endl;
    system("pause");
    return 0;
}

 输出结果

#include <iostream>
using namespace std;
int main() {
    
     //指定长度,后面没有赋值的元素,自动补0
    //既然自动补0了,那么你就是一个字符串了,字符串我们也可以单个输出
    
    char buf2[4] = { 'a','b','c' };
    cout <<"buf2:"<<buf2 << endl;
    cout << buf2[2] << endl;
    
    //c++中由" "包围的字符串会自动在末尾添加'\0'
    //所以我们说字符串用"",字符用''
    char buf3[6] = { "hello" };
    cout <<"buf3:"<< buf3 << endl;
    cout << buf3[1] << endl;//字符串也可以单个输出
    char buf4[6] = "hello";//这种形式更简洁实际开发中常用
    cout <<"buf4:"<< buf4 << endl;

    //也可以不指定长度
    char buf5[] = { "hello" };
    cout << "buf5:" << buf5 << endl;
    cout << buf5[1] << endl;//字符串也可以单个输出
    char buf6[] = "hello";//这种形式更简洁实际开发中常用
    cout <<"buf6:" << buf6 << endl;

    //需要留意一个坑,字符数组只有在定义时才能将整个字符串一次性地赋值给它,
     // 一旦定义完了,就只能一个字符一个字符地赋值了。请看下面的例子:
    char str[7];
    //str = "abc123";  //报错
    //正确
    str[0] = 'a'; str[1] = 'b'; str[2] = 'c';
    str[3] = '1'; str[4] = '2'; str[5] = '3';
    

    //如果只初始化部分数组元素,那么剩余的数组元素也会自动初始化为“零”值,
    // 所以我们只需要将 buf7 的第 0 个元素赋值为 0,剩下的元素就都是 0 了。
    //刚刚也说了碰见0就截止,后面有字符也不会输出
    //所有元素赋值为0
    char buf7[100] = { 0 };
    cout <<"buf7:" << buf7 << endl;
    //输出buf7会发现是空的,因为
    //'\0'是 ASCII 码表中的第 0 个字符,英文称为 NUL,
    // 中文称为“空字符”。该字符既不能显示,也没有控制功能,
    //输出该字符不会有任何效果,它在C语言中唯一的作用就是作为字符串结束标志。
    char buf8[100] = { '\0' };
    cout << "buf8:" << buf8 << endl;

    // 需要注意的一点是如果给数组的大小,因为字符串末尾自动默认补0,
    //所以定义数组大小时,记得至少比字符串的长度大1
    char buf9[3] = { '1','2','3' };//数组越界,应该是buf9[4]
    cout <<"buf9" << buf9 << endl;//乱码
    system("pause");
    return 0;
}

 布尔型 

#include<iostream>
using namespace std;

int main()
{
	//创建布尔数据类型
	bool flag = true;//true代表真
	cout << flag << endl;//输出:1
	//查看bool类型所占的内存空间
	flag = false;//false代表假
	cout << flag << endl;//输出:0
	cout << bool(8) << endl;
	//非0的数字都可以代表真,0表示假

	//2.查看bool类型所占的内存空间
	cout << sizeof(bool) << endl;//输出:1
	system("pause");

	return 0;

}

数据输入

#include<iostream>
using namespace std;

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

	//浮点型
	float f = 3.14f;
	cout << "请给浮点型变量f赋值:" << endl;
	cin >> f;
	cout << "浮点型变量f = " << f << endl;

	//字符型
	char ch;
	cout << "请给字符型变量f赋值:" << endl;
	cin >> ch;
	cout << "字符型变量ch = " << ch<< endl;

	//字符串型
	string st;
	cout << "请给字符串型变量f赋值:" << endl;
	cin >> st;
	cout << "字符串型变量st = " << st << endl;

	//bool型, bool类型(本质为整型) 只要非0的值都代表真
	bool flag;
	cout << "请给bool型变量flag赋值:" << endl;//输入100
	cin >> flag;
	cout << "bool型变量flag = " << flag << endl;//输出1
	system("pause");

	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值