2020-08-15

字符串类型


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

//char 变量名[] = "字符串值";多一个中括号,双引号""

int main() {
	//C语言字符串
	char str[] = "hello world";
	cout << str << endl;
	//C++风格字符串
	string str2 = "hello world";
	cout << str2 << endl;
	//需要添加一个头文件 #include <string>

	system("pause");
	return 0;
}

字符型

// char ch = 'a';字符型变量,只有一个字符,1个字节
//把字符对应的ASCII码放入到储存单元


#include<iostream>
using namespace std;
int main() {
	char ch = 'a';
	cout << ch << endl;
	cout << sizeof(ch) << endl;
	cout << int(ch) << endl;

	//错误示范:char ch2 = 'abc'; char ch2 ="a";
	//1,表达式 2,输出示范 3,占内存大小 4, 对应的ASCII值:a-97
	//ASCII值大致分为两部分:0-31控制字符,控制像打印机一些外部设施,32-126键盘上所能够找到的字符 
	system("pause");
	return 0;
}

转义字符

#include<iostream>
using namespace std;
int main() {
	//转义字符: 换行符\n 反斜杠\\ 水平制表\t
	cout << "hello world" << endl;
	cout << "hello world\n";
	cout << "\\" << endl;
	cout << "aaa\thelloworld" << endl;
	cout << "aaaa\thelloworld" << endl;
	cout << "aaaaaa\thelloworld" << endl;


	// 总结\n 用在C语言中没有自动换行的原因
	// 字符\\代表特殊字符反斜杠\
	// 字符\t占用8个位置,是后面的格式排列整齐

	system("pause");
	return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值