C++ Primer Plus 代码学习解析(第三章 3.5-3.7)

3.5 chartype.cpp

#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;
}
  1. 本代码主要演示了使用char类型储存字符的方法
  2. 由于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;
}
  1. 此代码探究不同字母对应的ASCII码
  2. char类型储存字符,int储存数字
  3. 此外,程序引用了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;
}
  1. 本代码主要探究\a等转义序列的使用,与C别无二致
  2. c++中换行同样是可以用\n
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值