在c++中cout相比于printf更加方便!!!数据类型:实型、字符型、转义字符、字符串型、布尔数据类型、数据的输入。

实型(浮点型)
作用:用于表示小数
浮点型变量分为两种:

  1. 单精度float(4字节)(7位有效数字)
  2. 双精度double(8字节)(15~16位有效数字)
    两者的区别在于表示的有效数字范围不同
    运行代码:
#include<iostream>
using namespace std;
int main() {
	float f1 = 3.14f;//通常在单精度数字后面添加一个f,否则系统默认是双精度
	cout << "f1 = " << f1 << endl;
	double d1 = 3.14;
	cout << "d1 = " << d1 << endl;
	//默认情况下,输出一个小数,会显示出6位有效数字
	float f2 = 3.1415926f;
	cout << "f2 = " << f2 << endl;
	double d2 = 1234567890;
	cout << "d2 = " << d2 << endl;
	//统计所占内存空间
	cout << "float所占内存空间为:" << sizeof(float) <<"个字节" << endl;
	cout << "double所占内存空间为:" << sizeof(double) << "个字节" << endl;
	//科学计数法
	float f3 = 3e2;
	cout << "f3 = 3e2=" << f3 << endl;
	float f4 = 3e-2;
	cout << "f4 = 3e-2=" << f4 << endl;
	system("pause");
	return 0;

运行结果:

字符型
作用:变量用于显示单个字符
语法:char ch = ‘a’

  • C/C++中字符型变量只占用1个字节
  • 字符型变量并不是把字符本身放到内存中存储,二十将对应的ASCll编码放入到存储单元中

运行代码:

#include<iostream>
using namespace std;
int main() {
	//创建字符
	char ch = 'a';
	char ch1 = 'A';
	cout << ch << endl;
	//字符型变量所占内存大小
	cout << "字符型变量所占内存大小为:" << sizeof(char) << endl;
	//字符型变量常见错误:创建字符型变量时要用单引号,且单引号里只能有单个字符 
	//字符型变量对应ASCll码
	cout << "a的SACll码值为:" << (int)ch << endl;
	cout << "A的SACll码值为:" << (int)ch1 << endl;
	system("pause");
	return 0;

运行结果:

转义字符

运行代码

#include<iostream>
using namespace std;
int main() {
	//转义字符
	//换行符  \n
	cout << "hello world  !!" << endl;
	cout << "hello world  !!!!\n" ;
	//反斜杠  \\:
	cout << "\\" << endl;
	//水平制表符  \t    作用:整齐的输出数据
	cout << "a\t张三" << endl;
	cout << "aa\t李聂霜玉" << endl;
	cout << "aaa\t肉肉" << endl;
	cout << "aaaa\t喵~" << endl;
	system("pause");
	return 0;

运行结果

字符串型

作用:用于表示一串字符

表示: string 变量名=“字符串值”

此处会对第11行报错,我在网上查找的答案是因为:printf只能输出C语言中的内置数据,string不是c语言内置数据。所以此处要将输出方式printf改为cout

运行代码为

#include<iostream>
#include<string>//使用C++风格的字符串的时候,要包含这个头文件
#include<stdio.h>
using namespace std;
int main() {
	//C风格字符串,注意,char 字符串名后面要加[],否则会被认为是创建的字符(等号后面应该是单引号)
	char str1[] = "C风格字符串 HELLO WORLD";
	printf("%s\n", &str1);         //可正常输出
	printf(str1);                  //可正常输出
	printf("\n");
	//C++风格字符串
	string str2 = "C++风格字符串hello world";
	cout << str2 << endl;	
	system("pause");
	return 0;

运行结果:

布尔数据类型(bool类型只占用1个字节的大小)
作用:代表真或假的值

  • true—真(本质是1)
  • false—假(本质是0)
#include<iostream>
using namespace std;
int main() {
	bool flag = true;
	cout << "true代表的是:" << flag << endl;
	bool flag2 = false;
	cout << "false代表的是" << flag2 << endl;
	//查看bool类型所占字节数
	cout << "bool类型所占字节数为:" << sizeof(bool) << endl;
	system("pause");
	return 0;

运行结果:

数据的输入

作用:用于从键盘获取数据

关键字:cin

语法:cin<<

运行代码:

#include<iostream>
#include<string>
using namespace std;
int main() {
	int a = 0;
	cout << "请给整型变量a赋值:" << endl;
	cin >> a;
	cout << "整型变量a = " << a << endl;
	//浮点型
	float b = 3.14f;
	cout << "请给浮点型变量b赋值:" << endl;
	cin >> b;
	cout << "浮点型变量b = " << b << endl;
	//字符型
	char c = 'M';
	cout << "请给字符型变量c赋值:" << endl;
	cin >> c;
	cout << "字符型变量c = " << c << endl;
	//字符串型
	string d = "liumiao";
	cout << "请给字符串型变量d赋值:" << endl;
	cin >> d;
	cout << "字符串型变量d = " << d << endl;
	//布尔类型
	bool e = "true";
	cout << "请给布尔类型变量e赋值:" << endl;
	cin >> e;
	cout << "布尔类型变量e = " << e << endl;