字符变量
char
字符类型:位数 8位 bit
使用8个二进制表示一个char
空间:一个字节
取值范围:-128到127
七个一等于一后面七个零减一 1111111 (10000000-1) 2^7-1 )
wchar 宽字符类型
uchar 无符号
ASCII码
65 a
97A
32 空格
A - " " = a;
#include<cmath>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num;
char ch1, ch2, ch3;
cout << "请输入一个数字:";
cin >> num;
//ch1 = cin.get();
//ch2 = cin.get();
//ch3 = cin.get();
cin >> ch1 >> ch2 >> ch3;
cout << num << "\t" << ch1 << "\t" << ch2 << "\t" << ch3 <<endl;
return 0;
}
string
#include<cmath>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//string 是字符串类型
string str = "你好!";
cout << str << endl;
return 0;
}
转义序列
#include<cmath>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//蜂鸣警报
cout << "\a"<< "\a" << "\a" << endl;
return 0;
}
\n
\t
\a
'
"
?
\
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double attack1 = 272;
double attack2 = 250;
double attack3 = 240;
//右对齐
cout << setw(8) <<attack1 <<
setw(8) <<attack2 <<
setw(8) <<attack3 <<endl;
//左对齐
cout <<left;
cout << setw(8) <<attack1 <<
setw(8) <<attack2 <<
setw(8) <<attack3 <<endl;
//填充
cout << setfill('_'); //默认用空格来填充,这里用下划线
cout << setw(8) <<attack1 <<
setw(8) <<attack2 <<
setw(8) <<attack3 <<endl;
return 0;
}
网友评论