C++Primer(5th) Exercises Section 2.1.3

Exercise 2.5: Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:

(a) ‘a’, L’a’, “a”, L”a”
(b) 10, 10u, 10L, 10uL, 012, 0xC
(c) 3.14, 3.14f, 3.14L
(d) 10, 10u, 10., 10e-2

Answers:

(a) ‘a’, char L’a’, long char “a”, string L”a” long string
(b) 10, integer 10u, unsigned integer 10L, long integer 10uL, unsigned long integer 012, octal integer 0xC hexadecimal integer
(c) 3.14, double 3.14f, float 3.14L long double
(d) 10, integer 10u, unsigned integer 10., double 10e-2 double

Exercise 2.6: What, if any, are the differences between the following

definitions:
int month = 9, day = 7;
int month = 09, day = 07;

Answers:

int month = 9, day = 7; decimal integer
int month = 09, day = 07; octal integer

Exercise 2.7: What values do these literals represent? What type does each have?
(a) “Who goes with F\145rgus?\012”
(b) 3.14e1L
(c) 1024f
(d) 3.14L

Answers:

(a) “Who goes with F\145rgus?\012” : value: Who goes with Fergus? type: string
(b) 3.14e1L value: 34.1 type: long double
(c) 1024f building error, should 1024.0f value: 1024 type: float
(d) 3.14L value : 3.14 type: long double

Exercise 2.8: Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.
Answers:
1)
#include <iostream>
int main()
{
std::cout<< "2\x4d\n" <<std::endl;
return 0;
}

2)
#include <iostream>
int main()
{
std::cout<< "2\t\x4d\n" <<std::endl;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值