C++ Primer, 5th Edition Exercise CH2

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?


Answer:

The difference is the size.But,the number of bits in—the arithmetic types varies across machines.so there is no guarantee the exact bits of the built-in types.The standard guarantees minimum sizes :int 16bits,long 32bits,long long 64bits,short 16bits.
A signed type represents negative or positive numbers (including zero);an unsigned type represents only values greater than or equal to zero.In an unsigned type, all the bits represent the value. 
By size ,float 32bits double 64bits.The float and double types typically yield about 7and 16 significant digits.


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.

Answer:
double for all,because they all contain decimal digitals,double is preciser than float.


Exercise 2.3: What output will the following code produce?


std::cout << u2 - u << std::endl; 32
std::cout << u - u2 << std::endl; 4294967264   32bit-int
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl; 32
std::cout << i - i2 << std::endl; -32
std::cout << i - u << std::endl; 0
std::cout << u - i << std::endl; 0


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) 'a' represents a character literal with a single character a. 

    L'a' represents a wide character literal with a single character a. 

    "a" represents a character string literal which is an array of two characters with letter a and the null character. 

    L"a" represents a wide string literal which is an array of two constant wide characters with a wide character a and a wide null character.


(b) 10 represents a literal integer constant, type is int. 

     10u represents a literal unsigned integer constant, type is unsigned int. 

     10L represents a literal integer constant, type is long. 

      10uL represents a literal unsigned integer constant, type is unsigned long. 

       012 represents a literal octal integer constants, type is int. 

       0xC represents a literal hexadecimal integer constant, type is int.
(c) 3.14 represents a floating-point literal, type is double. 

      3.14f represents a floating-point literal, type is float. 

       3.14L represents a floating-point literal, type is double.


(d)10 represents a literal integer constant, type is int. 

     10u represents a literal unsigned integer constant, type is unsigned int. 

     10. represents a floating-point literal include a decimal point,type is double.

     10e-2represents a floating-point literal using scientific notation,type is double.


Exercise 2.6: What, if any, are the differences between the following
definitions:
int month = 9, day = 7;
int month = 09, day = 07;


Integer literals that begin with 0 (zero) are interpreted as octal. so,int  month =09  is  a syntax errorr.


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   type is string

(b)31.4  type is long double.

(c) syntax error.

(d)3.14 type is 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.

#include<iostream>
int main()
{

	std::cout<<"2M\n";

	system("pause");
	return 0;
}

#include<iostream>
int main()
{

	std::cout<<"2\tM\n";

	system("pause");
	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) int my be the rval of >>.       int input_value;  std::cin>>input_value;

(b)error: narrowing conversion required  int i=3.14;

(c)error,wage undefined when salary=wage,   double wage = 9999.99,salary = wage;

(d)ok: but value will be truncated


Exercise 2.10: What are the initial values, if any, of each of the following variables?

std::string global_str;
int global_int;
int main()
{
int local_int;
std::string local_str;
}

global_str,local_str implicitly initialized to the empty string.

global_int defined outside any function body is initialized to zero.

local_int is not initialized.The value of an uninitialized variable of built-in type is undefined.


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 :Any declaration that includes an explicit initializer is a definition.

(b) definition.

(c) declaratrion. To obtain a declaration that is not also a definition, we add the extern keyword and may not provide an explicit initializer:


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)(b)(c)(d).


Exercise 2.13: What is the value of j in the following program?
int i = 42;
int main()
{
int i = 100;
int j = i;
}

j=100.


Exercise 2.14: Is the following program legal? If so, what values are printed?

int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;

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) invalid. the type of ival is int,assign int to floating-point need explicit casting.

(b) invalid.initializer must be an object

(c)valid.

(d)valid.


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,assign 3.14159 to d.

(b) valid ,assign value of r1 to d.

(c)invalid. the type of i is int,assign int to double need explicit casting.

(d)invalid. the type of i is int,assign int to double need explicit casting.


Exercise 2.17: What does the following code print?

int i, &ri = i;
i = 5; ri = 10;
std::cout << i << " " << ri << std::endl;

10  10


Exercise 2.18: Write code to change the value of a pointer. Write code to change the value to which the pointer points.

#include <iostream>
using namespace std;
 
void main()
{
       // declare and initialize two float variables
       float var1 = 58.98;
       float var2 = 70.44;
       // declare a float pointer variable
       float *ptr_var;
       // make ptr_var point to variable var1...
       ptr_var = &var1;
       // prints 58.98
       cout<<"\nThe first value is(var1) "<<*ptr_var;
       cout<<"\nThe address of the first data is "<<ptr_var<<"\n";
       cout<<"\nThen let the same pointer (*ptr_var)";
       cout<<"\npoint to other address...\n";
       // make ptr_var point to variable var2...
       ptr_var = &var2;
       // prints 70.44
       cout<<"\nThe second value is(var2) "<<*ptr_var;
       cout<<"\nThe address of the second data is "<<ptr_var<<endl;
	   system("pause");
}


Exercise 2.19: Explain the key differences between pointers and references.

Unlike a reference, a pointer is an object in its own right. Pointers can be assigned and copied; a single pointer can point to
several different objects over its lifetime. Unlike a reference, a pointer need not be initialized at the time it is defined.


Exercise 2.20: What does the following program do?
int i = 42;
int *p1 = &i;
*p1 = *p1 * *p1;

Answer: p1 point to i, i=i*i.


Exercise 2.21: Explain each of the following definitions. Indicate whether any are illegal and, if so, why.
int i = 0;

(a) double* dp = &i;
(b) int *ip = i;
(c) int *p = &i;

(a) 不合法,无法从 int *转换成 double*。

(b)不合法,无法从int转换成int *。

(c)合法。


Exercise 2.22: Assuming p is a pointer to int, explain the following code:
if (p) // ...
if (*p) // ...

if(p) // 如果p是非空指针

if(*p)  //如果 p指针的内容是非0


Exercise 2.23: Given a pointer p, can you determine whether p points to a valid object? If so, how? If not, why not?

if(NULL==p)


Exercise 2.24: Why is the initialization of p legal but that of lp illegal?

int i = 42; void *p = &i; long *lp = &i;

void type can hold the address of any object. but long can not match with int.


Exercise 2.25: Determine the types and values of each of the following variables.
(a) int* ip, &r = ip; //ip is a point to int , r is the reference of ip.
(b) int i, *ip = 0;  //i is an int variable ,ip is an int point to 0.
(c) int* ip, ip2; ip is an int point ,ip2 is an int varible.


Exercise 2.26: Which of the following are legal? For those that are illegal,
explain why.
(a) const int buf;
(b) int cnt = 0;
(c) const int sz = cnt;
(d) ++cnt; ++sz;


(b)(c) are legal

(a) const variable must be initialized.

(d)const variable is unmodifiable.



Exercise 2.27: Which of the following initializations are legal? Explain why.
(a) int i = -1, &r = 0;
(b) int *const p2 = &i2;
(c) const int i = -1, &r = 0;
(d) const int *const p3 = &i2;
(e) const int *p1 = &i2;
(f) const int &const r2;
(g) const int i2 = i, &r = i;

(a) illegal,initializer must be an object.
(b)legal. we can use const pointer point to non const object.

(c)legal.A Reference to const May Refer to an Object That Is Not const

(d)legal

(e)legal

(f)illegal.

(g)legal


Exercise 2.28: Explain the following definitions. Identify any that are illegal.
(a) int i, *const cp;
(b) int *p1, *const p2;

(c) const int ic, &r = ic;
(d) const int *const p3;
(e) const int *p;

(a) illegal.const object must be initialized.

(b) illegal the same to (a)

(c) illegal .ic must be initialized.

(d) illegal .

(e)legal.


Exercise 2.29: Uing the variables in the previous exercise, which of the
following assignments are legal? Explain why.
(a) i = ic;
(b) p1 = p3;
(c) p1 = &ic;
(d) p3 = &ic;
(e) p2 = p1;
(f) ic = *p3;

(a)(b)(d)(f) legal.

(c)(e) illegal.


Exercise 2.30: For each of the following declarations indicate whether the object being declared has top-level or low-level const.

const int v2 = 0; int v1 = v2;
int *p1 = &v1, &r1 = v1;
const int *p2 = &v2, *const p3 = &i, &r2 = v2;



Exercise 2.31: Given the declarations in the previous exercise determine
whether the following assignments are legal. Explain how the top-level or
low-level const applies in each case.
Click here to view code image
r1 = v2;
p1 = p2; p2 = p1;
p1 = p3; p2 = p3;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值