Chapter 2. Variables and Basic Types
====================================
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_04.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 << "\062\115\012";
std::cout << "\062\t\115\012";
}
```
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): error: type 'double' cannot be narrowed to 'int' in initializer list.
```cpp
double i = { 3.14 };
```
(c): if declared 'wage' before, it's right. Otherwise, error would be happened: `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 information.
> Uninitialized objects of built-in type defined inside a function body have a undefined value. Objects of class type that we do not explicitly initialize 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`. cause 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;
```
Yes. It is legal.Printed:`100, 45.`
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.
>
> ```cpp
> 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):
没有合适的资源?快使用搜索试试~ 我知道了~
C++ Primer 第五版答案(全书各章节代码)
共605个文件
cpp:402个
h:165个
md:17个
需积分: 50 43 下载量 13 浏览量
2016-04-21
10:03:20
上传
评论
收藏 448KB ZIP 举报
温馨提示
C++ Primer 第五版答案(全书各章节代码),包含全书各个章节的代码。
资源推荐
资源详情
资源评论
收起资源包目录
C++ Primer 第五版答案(全书各章节代码) (605个子文件)
Sales_data.cc 3KB
fact.cc 296B
factMain.cc 189B
.clang-format 2KB
main.cpp 7KB
main.cpp 6KB
strvec.cpp 6KB
ex14_18_String.cpp 5KB
ex14_26_String.cpp 5KB
ex14_49.cpp 5KB
ex14_24.cpp 5KB
main.cpp 4KB
ex14_16_String.cpp 4KB
main.cpp 4KB
main.cpp 4KB
ex13_49_Message.cpp 4KB
Sales_data.cpp 4KB
ex14_23.cpp 4KB
main.cpp 3KB
ex14_18_StrVec.cpp 3KB
ex14_26_StrVec.cpp 3KB
ex14_27_28_StrBlob.cpp 3KB
ex14_30_StrBlob.cpp 3KB
ex14_18_StrBlob.cpp 3KB
ex14_26_StrBlob.cpp 3KB
main.cpp 3KB
ex10_34_35_36_37.cpp 3KB
ex14_16_StrVec.cpp 3KB
ex15.31.32.33.cpp 3KB
main.cpp 3KB
ex12_07.cpp 3KB
textquery.cpp 3KB
main.cpp 3KB
ex13_49_StrVec.cpp 3KB
main.cpp 3KB
main.cpp 2KB
main.cpp 2KB
ex3_16.cpp 2KB
main.cpp 2KB
ex12_17_18.cpp 2KB
ex13_40.cpp 2KB
main.cpp 2KB
main.cpp 2KB
wy_textquery.cpp 2KB
ex13_39.cpp 2KB
ex13_42_StrVec.cpp 2KB
textquery.cpp 2KB
textquery.cpp 2KB
ex9_51.cpp 2KB
ex15.23.cpp 2KB
main.cpp 2KB
ex13_34_36_37.cpp 2KB
ex9_43.cpp 2KB
ex14_40.cpp 2KB
ex12_28.cpp 2KB
ex11_27_28_29_30.cpp 2KB
main.cpp 2KB
main.cpp 2KB
main.cpp 2KB
main.cpp 2KB
ex8_13.cpp 2KB
ex12_33.cpp 2KB
main.cpp 2KB
ex10_18_19.cpp 2KB
ex12_06.cpp 2KB
wy_queryresult.cpp 2KB
ex12_32.cpp 2KB
textquery.cpp 2KB
textquery.cpp 2KB
ex14_16_StrBlob.cpp 2KB
ex12_27_30.cpp 2KB
ex15.8.9.10.cpp 2KB
ex_33.cpp 2KB
ex5_12.cpp 2KB
main.cpp 2KB
ex11_14.cpp 2KB
ex11_8.cpp 2KB
main.cpp 2KB
main.cpp 2KB
ex15.24.25.cpp 2KB
ex10_20_21.cpp 2KB
main.cpp 2KB
ex11_38.cpp 2KB
main.cpp 2KB
main.cpp 2KB
ex12_12.cpp 1KB
main.cpp 1KB
ex11_24_25_26.cpp 1KB
main.cpp 1KB
ex4_28.cpp 1KB
main.cpp 1KB
ex3_01b.cpp 1KB
ex13_42_TextQuery.cpp 1KB
ex14_44.cpp 1KB
ex14_38_39.cpp 1KB
ex6_54_55_56.cpp 1KB
ex11_33.cpp 1KB
ex13_13.cpp 1KB
main.cpp 1KB
ex11_7.cpp 1KB
共 605 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
珠峰长啸
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功