##Exercise 2.1
> What are the differences between int, long, long long,
and short? Between an unsigned and a signed type? Between a float and
a double?
C++ guarantees `short` and `int` is **at least** 16 bits, `long` **at least** 32 bits, `long long` **at least** 64 bits.
The `signed` can represent positive numbers, negative numbers and zero, while `unsigned` can only represent numbers no less than zero.
The C and C++ standards do not specify the representation of float, double and long double.
It is possible that all three implemented as IEEE double-precision.
Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE **single-precision** floating point number (binary32),
and double is a IEEE **double-precision** floating point number (binary64).
Usage:
- Use `int` for integer arithmetic. `short` is usually too small and, in practice,
`long` often has the same size as `int`. If your data values are larger than
the minimum guaranteed size of an `int`, then use `long long`.
(In a word: short < **int** < long < long long)
- Use an unsigned type when you know that the values cannot be negative.
(In a word: no negative, unsigned.)
- Use double for floating-point computations; float usually does not have
enough precision, and the cost of double-precision calculations versus
single-precision is negligible. In fact, on some machines, double-precision
operations are faster than single. The precision offered by long double
usually is unnecessary and often entails considerable run-time cost.
(In a word: float < **double** < long double)
Reference:
- [What are the criteria for choosing between short / int / long data types?](http://www.parashift.com/c++-faq/choosing-int-size.html)
- [Difference between float and double](http://stackoverflow.com/questions/2386772/difference-between-float-and-double)
- Advice: Deciding which Type to Use(This book.)
##Exercise 2.2
>To calculate a mortgage payment, what types would you use
for the rate, principal, and payment? Explain why you selected each type.
use `double`, or also `float`.
The rate most like that: 4.50 % per year.
The principal most like that: $854.36
The payment most like that: $1,142.36
Reference:
- [mortgage-calculator](http://www.bankrate.com/calculators/mortgages/mortgage-calculator.aspx)
- [What's in a Mortgage Payment?](http://www.homeloanlearningcenter.com/mortgagebasics/whatsinamortgagepayment.htm)
##Exercise 2.3
> What output will the following code produce?
```cpp
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl;
std::cout << i - i2 << std::endl;
std::cout << i - u << std::endl;
std::cout << u - i << std::endl;
```
Output(g++ 4.8):
```
32
4294967264
32
-32
0
0
```
##Exercise 2.4
> Write a program to check whether your predictions were
correct. If not, study this section until you understand what the problem is.
[Here](ex2_4.cpp) is the code, please test it in your computer.
##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
(a): character literal, wide character literal, string literal, string wide character literal.
(b): decimal, unsigned decimal, long decimal, unsigned long decimal, octal, hexadecimal.
(c): double, float, long double.
(d): decimal, unsigned decimal, double, double.
##Exercise 2.6
> What, if any, are the differences between the following
definitions:
```cpp
int month = 9, day = 7;
int month = 09, day = 07;
```
The first line's integer is decimal.
The second line:
1. `int month = 09` is invalid, cause octal don't have digit `9`.
2. `day` is octal.
##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
(a): Who goes with Fergus?(new line) "string"
(b): 31.4 "long double"
(c): 1024 "float"
(d): 3.14 "long double"
Reference:
- [ASCII Table](http://www.asciitable.com/)
##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.
```cpp
#include <iostream>
int main()
{
std::cout << 2 << "\115\012";
std::cout << 2 << "\t\115\012";
return 0;
}
```
##Exercise 2.9
>Explain the following definitions. For those that are illegal,
explain what’s wrong and how to correct it.
- (a) std::cin >> int input_value;
- (b) int i = { 3.14 };
- (c) double salary = wage = 9999.99;
- (d) int i = 3.14;
(a): error: expected '(' for function-style cast or type construction.
```cpp
int input_value = 0;
std::cin >> input_value;
```
(b):---when you compile the code without the argument "-std=c++11", you will get the warning below:
warning: implicit conversion from 'double' to 'int' changes value from 3.14 to 3.
---when you compile the code using "-std=c+11", you will get a error below:
error: type 'double' cannot be narrowed to 'int' in initializer list
---conclusion: Obviously, list initialization becomes strict in c++11.
```cpp
double i = { 3.14 };
```
(c): --if you declared 'wage' before, it's right. Otherwise, you'll get a error:
error: use of undeclared identifier 'wage'
```cpp
double wage;
double salary = wage = 9999.99;
```
(d): ok: but value will be truncated.
```cpp
double i = 3.14;
```
##Exercise 2.10
>What are the initial values, if any, of each of the following variables?
```cpp
std::string global_str;
int global_int;
int main()
{
int local_int;
std::string local_str;
}
```
`global_str` is global variable, so the value is empty string.
`global_int` is global variable, so the value is zero.
`local_int` is a local variable which is not uninitialized, so it has a undefined value.
`local_str` is also a local variable which is not uninitialized, but it has a value that is defined by the class. So it is empty string.
PS: please read P44 in the English version, P40 in Chinese version to get more.
The note: Uninitialized objects of built-in type defined inside a function body have a undefined value. Objects of class type that we do not explicitly inititalize have a value that is defined by class.
##Exercise 2.11
> Explain whether each of the following is a declaration or a
definition:
- (a) extern int ix = 1024;
- (b) int iy;
- (c) extern int iz;
(a): definition.
(b): definition.
(c): declaration.
##Exercise 2.12
>Which, if any, of the following names are invalid?
- (a) int double = 3.14;
- (b) int _;
- (c) int catch-22;
- (d) int 1_or_2 = 1;
- (e) double Double = 3.14;
`a`, `c`, `d` are invalid.
##Exercise 2.13
>What is the value of j in the following program?
```cpp
int i = 42;
int main()
{
int i = 100;
int j = i;
}
```
`100`, since the global `i` was hidden by the local `i`.
##Exercise 2.14
>Is the following program legal? If so, what values are printed?
```cpp
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
```
Legal. Output:
```100 45```
Note: Such naming is considered as bad practise.
##Exercise 2.15
>Which of the following definitions, if any, are invalid? Why?
- (a) int ival = 1.01;
- (b) int &rval1 = 1.01;
- (c) int &rval2 = ival;
- (d) int &rval3;
```
(a): valid.
(b): invalid. initializer must be an object.
(c): valid.
(d): invalid. a reference must be initialized.
```
##Exercise 2.16
>Which, if any, of the following assignments are invalid? If they are valid, explain what they do.
int i = 0, &r1 = i; double d = 0, &r2 = d;
- (a) r2 = 3.14159;
- (b) r2 = r1;
- (c) i = r2;
- (d) r1 = d;
```
(a): valid. let d equal 3.14159.
(b): valid. automatic convert will happen.
(c): valid. but value will be truncated.
(d): valid. but value will be truncated.
```
##Exercise 2.17
>What
没有合适的资源?快使用搜索试试~ 我知道了~
C++ Primer 英文版答案
共1079个文件
cpp:413个
cc:260个
h:200个
需积分: 10 13 下载量 122 浏览量
2015-08-18
07:07:40
上传
评论
收藏 1.8MB ZIP 举报
温馨提示
由github上获取的由第三方提供的 C++ Primer(Stanley B. Lippman) 英文版答案.
资源推荐
资源详情
资源评论
收起资源包目录
C++ Primer 英文版答案 (1079个子文件)
absInt 50B
absInt 50B
accum 69B
add 5B
add 5B
add_item 135B
add_item 44B
add_item 44B
add_item 44B
add_item 44B
add_item 44B
add_item_data 180B
Alice 6B
Alice 6B
alloc 18B
badgetc 10B
runpgms.bat 203B
bits 9B
book_sales 224B
book_sales 224B
book_sales 224B
book_sales 224B
book_sales 176B
book_trans 44B
list.c 883B
fraction.c 766B
euclid.c 414B
euclid-mod.c 376B
sieve.c 298B
StrFolder.cc 6KB
Folder.cc 6KB
bits.cc 6KB
TextQuery.cc 5KB
multidim.cc 5KB
newcount-size.cc 5KB
char_repl.cc 4KB
newcount-size.cc 4KB
findbook.cc 4KB
word_transform.cc 4KB
ifgrades.cc 4KB
String.cc 4KB
calc.cc 4KB
Basket_main.cc 4KB
useScreen.cc 4KB
Query.cc 4KB
variadic3.cc 4KB
Sales_data.cc 4KB
vec_decls.cc 4KB
str_assignOps.cc 4KB
Sales_data.cc 4KB
pair.cc 3KB
refparms.cc 3KB
map1.cc 3KB
oknew.cc 3KB
sstream.cc 3KB
rand1.cc 3KB
Array.cc 3KB
calc.cc 3KB
errMsg_initList.cc 3KB
HasPtrUseCount.cc 3KB
validatePhones.cc 3KB
usealloc.cc 3KB
Sales_data.cc 3KB
Sales_data.cc 3KB
vecSubs.cc 3KB
elimDups.cc 3KB
arrRet.cc 3KB
FolderMain.cc 3KB
unorderedWordCount.cc 3KB
StrFolderMain.cc 3KB
virtualsNscope.cc 3KB
expansion.cc 3KB
erase2.cc 3KB
Basket.cc 3KB
rand2.cc 3KB
seek.cc 3KB
fcnobj.cc 3KB
multimap.cc 3KB
cstring_hdr.cc 3KB
reverse.cc 3KB
dyn_cast.cc 3KB
variadic.cc 3KB
capacity.cc 3KB
erase1.cc 3KB
lambda.cc 3KB
useStrVec.cc 3KB
regex1.cc 3KB
setOps.cc 3KB
unsigned.cc 3KB
phonesFinal.cc 3KB
sizeof_pgm.cc 3KB
tuple.cc 3KB
fcnptrRet.cc 3KB
manips.cc 3KB
trail-ret.cc 3KB
use_find.cc 3KB
rand6.cc 3KB
enum.cc 3KB
cctype.cc 3KB
vecScores.cc 3KB
共 1079 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
Pallad
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功