#include <iostream>
int main()
{
using namespace std;
char ch;
cout << "Enter a character: " << endl;
cin >> ch;
cout << "Thank you for the " << ch << " character " << endl;
return 0;
}
本代码主要演示了使用char类型储存字符的方法
由于char类型较小(128),比short还要小,所以也可以让其处理较小的整型
3.6 morechar.cpp
#include <iostream>
int main()
{
using namespace std;
char ch = 'M';
int i = ch;
cout << "The ASCLL code for " << ch << " is " << i << endl;
cout << "Add one to the character code: " << endl;
ch += 1;
i = ch;
cout << "The ASCLL code for " << ch << " is " << i << endl;
cout << "Displaying char ch using cout.put(ch): " ;
cout.put(ch);
cout.put('!');
cout << endl << "Done!" << endl;
return 0;
}
此代码探究不同字母对应的ASCII码
char类型储存字符,int储存数字
此外,程序引用了cout的一个特性——cout.put函数,该函数显示一个字符常量
3.7 bondini.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "\aH\"ello\" \n" ;
cout << "Enter a code:______\b\b\b\b\b " << endl;
long code;
cin >> code;
cout << "You entered " << code << " ...\n ";
return 0;
}