C++ Primer #02 Variables and Basic Types

C++
C++ Primer Chapter 2

2. Variables and Basic Types

2.1 Primitive Built-in Types

Exercise 2.3:

What output will the following code produce?


Console:

 

Exercise 2.5:

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


The answer is the following:

👍 This is a good exercise which can explain everything in literals.

 

 

Exercise 2.6:

What, if any, are the differences between the following definitions


Answer:

The first row is in decimal while the second row is in octal. Hence, int month = 09 is invalid which surpass 7(octal varies from 0-7).

 

Exercise 2.7:

What values do these literals represent? What type does each have?


(a) \145 means e , \012 means \n which changes line.

(b) 3.14e1L means long double. e1 means 10^1. Therefore, it will be 31.4.

(c) ⚠️ERROR. f stands for float. Hence, it should be 1024.0f.

(d) 3.14 Long Double.

 

2.2 Variables

Exercise 2.9:

Explain the following definitions. For those that are illegal, explain what’s wrong and how to correct it.


(a) ⚠️ERROR. The variable should be initialized before feeding values. It should be:

(b) ⚠️Type double cannot be narrowed to int in initializer list. It should be:

 

(c) ⚠️ERROR. wage should be declared and initialized before salary. It should be:

The wage and salary will have same value which are 9000.

(d) ✔️OK. i will be 3. Better solution should be:

 

Exercise 2.10:

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


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 uninitialized, so it has a undefined value.

local_str is also a local variable which is uninitialized, but it has a value that is defined by the class. So it is empty string.

‼️The RULE is 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.

Hence, the output of

is :

 

Exercise 2.11:

Explain whether each of the following is a declaration or a definition


(a) Definition.

(b) Definition.

(c) Declaration.

 

Exercise 2.12:

Which, if any, of the following names are invalid?


(a)

(b) ✔️. But it is not a good name.

(c)

(d)

(e) ✔️. But it is not a good variable name since it would be confused with the type ofdouble.

 

Exercise 2.13

What is the value of j in the following program?


j is 100. Because the local i assigns its value to j.

 

Exercise 2.14:

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


It should print out:

The i inside the scope of main is a local variable. Hence, in the for loop, i != 100. While outside of scope, it is 100 .

 

2.3 Compound Types

Exercise 2.15:

Which of the following definitions, if any, are invalid? Why?


(a) ✔️

(b) Does not reference any object.

(c) ✔️

(d) Does not initialized.

 

Exercise 2.16:

Which, if any, of the following assignments are invalid? If they are valid, explain what they do.


(a) ✔️. d also equals to 3.14

(b) ✔️. All are 0.

(c) ✔️. All are 0.

(d) ✔️. All are 0.

 

Exercise 2.17:

What does the following code print?


Since ri is the reference of i, so i and ri are both equal to 10.

 

Exercise 2.18:

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


Console:

 

 

Exercise 2.19:

Explain the key differences between pointers and references.


  1. A reference is another name of an already existing object. a pointer is an object in its own right.
  2. Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. a pointer can be assigned and copied.
  3. a reference always get the object to which the reference was initially bound. a single pointer can point to several different objects over its lifetime.
  4. a reference must be initialized. a pointer need not be initialized at the time it is defined.

 

 

Exercise 2.20:

What does the following program do?


The program changes the valuei pointed by p1 to 42*42 by dereference.

 

 

Exercise 2.21:

Explain each of the following definitions. Indicate whether any are illegal and, if so, why


(a) Different type. More technical definition is: cannot initialize a variable of type double * with an rvalue of type int *

(b) Invalid syntax. More technical definition is: cannot initialize a variable of type int * with an lvalue of type int

(c) ✔️ OK.

 

 

Exercise 2.22:

Assuming p is a pointer to int, explain the following code:


(1) First line: whether p is nullptr? So there may be 2 solutions: true - p is not nullptr, false - p is nullptr.

(2) Second line: whether *p is zero? So there may be 2 solutions: true - *p is not zero, false - *p is zero.

Console:

Hence, nullptr is false.

 

 

Exercise 2.24:

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


Because void * is special which can the address of any object. While long * cannot.

 

Exercise 2.25:

Determine the types and values of each of the following variables.


(a) ip is a pointer. i is an int with undefined value. r is a reference of i.

(b) i is an int with undefined value. ip is a pointer, especially nullptr.

(c) ip is a pointer. ip2 is an int.

 

 

2.4 const Qualifier

Exercise 2.26:

Which of the following are legal? For those that are illegal, explain why.

 

Previous
Previous

Unreal Engine C++ Developer #1

Next
Next

C++ Primer #01 Getting Started