C++ Primer #02 Variables and Basic Types
2. Variables and Basic Types
2.1 Primitive Built-in Types
Exercise 2.3:
What output will the following code produce?
xxxxxxxxxx
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;
Console:
xxxxxxxxxx
32
4294967264
32
-32
0
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:
xxxxxxxxxx
(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
The answer is the following:
xxxxxxxxxx
(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.
👍 This is a good exercise which can explain everything in literals.
Exercise 2.6:
What, if any, are the differences between the following definitions
xxxxxxxxxx
int month = 9, day = 7;
int month = 09, day = 07;
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?
xxxxxxxxxx
(a) "Who goes with F\145rgus?\012"
(b) 3.14e1L
(c) 1024f
(d) 3.14L
(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.
xxxxxxxxxx
(a) std::cin >> int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14;
(a) ⚠️ERROR. The variable should be initialized before feeding values. It should be:
xxxxxxxxxx
int input_value;
std::cin >> input_value;
(b) ⚠️Type double
cannot be narrowed to int
in initializer list. It should be:
xxxxxxxxxx
double i = {3.14};
(c) ⚠️ERROR. wage
should be declared and initialized before salary. It should be:
xxxxxxxxxx
double wage = 700;
double salary = wage = 9000;
cout << wage << endl;
cout << salary << endl;
The wage
and salary
will have same value which are 9000
.
(d) ✔️OK. i
will be 3
. Better solution should be:
xxxxxxxxxx
double i = 3.14;
Exercise 2.10:
What are the initial values, if any, of each of the following variables?
xxxxxxxxxx
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 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
xxxxxxxxxx
std::string global_str;
int global_int;
int main()
{
int local_int;
std::string local_str;
cout << "--------------" << endl;
cout << global_str << endl;
cout << global_int << endl;
cout << local_str << endl;
cout << local_int << endl;
cout << "--------------" << endl;
return 0;
}
is :
xxxxxxxxxx
--------------
0
0
--------------
Exercise 2.11:
Explain whether each of the following is a declaration or a definition
xxxxxxxxxx
(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?
xxxxxxxxxx
(a) int double = 3.14;
(b) int _;
(c) int catch-22;
(d) int 1_or_2 = 1;
(e) double Double = 3.14;
(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?
xxxxxxxxxx
int i = 42;
int main()
{
int i = 100;
int j = i;
}
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?
xxxxxxxxxx
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
It should print out:
xxxxxxxxxx
100 45
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?
xxxxxxxxxx
(a) int ival = 1.01;
(b) int &rval1 = 1.01;
(c) int &rval2 = ival;
(d) int &rval3;
(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.
x
double d = 0, &r2 = d;
int i = 0, &r1 = i;
double d = 0, &r2 = d;
x(d) r1 = d;
(a) r2 = 3.14159;
(b) r2 = r1;
(c) i = r2;
(d) r1 = d;
(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?
x
int i, &ri = i; i=5; ri= 10;
std::cout << i << " " << ri << std::endl;
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.
x
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
int main()
{
int num = 10;
int NUM = 20;
int *pointer = #
//change the value of a pointer
pointer = &NUM;
//change the value to which the pointer points.
*pointer = 30;
cout << "num: " << num << endl;
cout << "NUM: " << NUM << endl;
cout << "pointer: " << pointer << endl;
return 0;
}
Console:
xxxxxxxxxx
num: 10
JBL: 30
pointer: 0x61fe10
Exercise 2.19:
Explain the key differences between pointers and references.
- A reference is another name of an already existing object. a pointer is an object in its own right.
- 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.
- 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.
- 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?
xxxxxxxxxx
int i = 42;
int *p1 = &i; *p1 = *p1 **p1;
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
x
int i = 0;
xxxxxxxxxx
(a) double* dp = &i;
(b) int *ip = i;
(c) int *p = &i;
(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:
xxxxxxxxxx
if (p) // ...
if (*p) // ...
(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
.
xxxxxxxxxx
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
int main()
{
int i=10;
int *j = nullptr; //equivalent to int *j=0;
int *k = &i;
if(i)
{
cout << "i out" << endl;
}
if(j)
{
cout << "j out" << endl; //nullptr=false
}
if(k)
{
cout << "k out" << endl;
}
return 0;
}
Console:
xxxxxxxxxx
i out
k out
Process returned 0 (0x0) execution time : 1.455 s
Press any key to continue.
Hence, nullptr
is false
.
Exercise 2.24:
Why is the initialization of p
legal but that of lp
illegal?
xxxxxxxxxx
int i = 42;
void *p = &i;
long *lp = &i;
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.
xxxxxxxxxx
(a) int* ip, i, &r = i;
(b) int i, *ip = 0;
(c) int* ip, ip2;
(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.
xxxxxxxxxx
(a) const int buf;
(b) int cnt = 0;
(c) const int sz = cnt;
(d) ++cnt; ++sz;