What are the differences between
int
,long
,long long
, andshort
? Between anunsigned
and asigned
type? Between afloat
and adouble
?
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 asint
. If your data values are larger than the minimum guaranteed size of anint
, then uselong 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?
- Difference between float and double
- Advice: Deciding which Type to Use(This book.)
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:
What output will the following code produce?
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
Write a program to check whether your predictions were correct. If not, study this section until you understand what the problem is.
Here is the code, please test it in your computer.
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.
What, if any, are the differences between the following definitions:
int month = 9, day = 7; int month = 09, day = 07;
The first line's integer is decimal.
The second line:
int month = 09
is invalid, cause octal don't have digit9
.day
is octal.
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:
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 << "\062\115\012";
std::cout << "\062\t\115\012";
}
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.
int input_value = 0;
std::cin >> input_value;
(b): error: type 'double' cannot be narrowed to 'int' in initializer list.
double i = { 3.14 };
(c): if declared 'wage' before, it's right. Otherwise, error would be happened: error: use of undeclared identifier 'wage'
double wage;
double salary = wage = 9999.99;
(d): Ok: but value will be truncated.
double i = 3.14;
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
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.
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.
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.
What is the value of j in the following program?
int i = 42; int main() { int i = 100; int j = i; }
100
. cause the global i
was hidden by the local i
.
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;
Yes. It is legal.Printed:100, 45.
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.
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.
What does the following code print?
int i, &ri = i; i = 5; ri = 10; std::cout << i << " " << ri << std::endl;
10, 10
Write code to change the value of a pointer. Write code to change the value to which the pointer points.
int a = 0, b = 1;
int *p1 = &a, *p2 = p1;
// change the value of a pointer.
p1 = &b;
// change the value to which the pointer points
*p2 = b;
Explain the key differences between pointers and references.
definition:
the pointer is "points to" any other type.
the reference is "another name" of an object.
key difference:
- 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.
Usage advise:
check here
What does the following program do?
int i = 42; int *p1 = &i; *p1 = *p1 * *p1;
p1
pointer to i
, i
's value changed to 1764(42*42)
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): illegal, cannot initialize a variable of type
double *
with an rvalue of typeint *
- (b): illegal, cannot initialize a variable of type
int *
with an lvalue of typeint
- (c): legal.
Assuming p is a pointer to int, explain the following code:
if (p) // whether p is nullptr? if (*p) // whether the value pointed by p is zero?
Given a pointer p, can you determine whether p points to a valid object? If so, how? If not, why not?
No. Because more information needed to determine whether the pointer is valid or not.
Why is the initialization of
p
legal but that oflp
illegal?int i = 42; void *p = &i; long *lp = &i;
Because the type void*
is a special pointer type that can hold the address of any object. But we cannot initialize a variable of type long *
with an rvalue of type int *
Determine the types and values of each of the following variables.
- (a)
int* ip, i, &r = i;
- (b)
int i, *ip = 0;
- (c)
int* ip, ip2;
- (a):
ip
is a pointer toint
,i
is anint
,r
is a reference toint
i
. - (b):
ip
is a valid, null pointer, andi
is anint
. - (c):
ip
is a pointer toint
, andip2
is anint
.
Which of the following are legal? For those that are illegal, explain why.
const int buf; // illegal, buf is uninitialized const. int cnt = 0; // legal. const int sz = cnt; // legal. ++cnt; ++sz; // illegal, attempt to write to const object(sz).
Which of the following initializations are legal? Explain why.
int i = -1, &r = 0; // illegal, r must refer to an object. int *const p2 = &i2; // legal. const int i = -1, &r = 0; // legal. const int *const p3 = &i2; // legal. const int *p1 = &i2; // legal const int &const r2; // illegal, r2 is a reference that cannot be const. const int i2 = i, &r = i; // legal.
Explain the following definitions. Identify any that are illegal.
int i, *const cp; // illegal, cp must initialize. const int ic, &r = ic; // illegal, ic must initialize. int *p1, *const p2; // illegal, p2 must initialize. const int *const p3; // illegal, p3 must initialize. const int *p; // legal. a pointer to const int.
Using the variables in the previous exercise, which of the following assignments are legal? Explain why.
i = ic; // legal. p1 = p3; // illegal. p3 is a pointer to const int. p1 = ⁣ // illegal. ic is a const int. p3 = ⁣ // illegal. p3 is a const pointer. p2 = p1; // illegal. p2 is a const pointer. ic = *p3; // illegal. ic is a const int.
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;
- v2 is top-level
const
, p2 is low-levelconst
. - p3: right-most
const
is top-level, left-most is low-level. - r2 is low-level
const
.
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.r1 = v2; // legal, top-level const in v2 is ignored. p1 = p2; // illegal, p2 has a low-level const but p1 doesn't. p2 = p1; // legal, we can convert int* to const int*. p1 = p3; // illegal, p3 has a low-level const but p1 doesn't. p2 = p3; // legal, p2 has the same low-level const qualification as p3.
Is the following code legal or not? If not, how might you make it legal?
int null = 0, *p = null;
illegal.
int null = 0, *p = nullptr;
Using the variable definitions from this section, determine what happens in each of these assignments:
a = 42; // set 42 to int a. b = 42; // set 42 to int b. c = 42; // set 42 to int c. d = 42; // ERROR, d is an int *. correct: *d = 42; e = 42; // ERROR, e is an const int *. correct: e = &c; g = 42; // ERROR, g is a const int& that is bound to ci.
Write a program containing the variables and assignments from the previous exercise. Print the variables before and after the assignments to check whether your predictions in the previous exercise were correct. If not, study the examples until you can convince yourself you know what led you to the wrong conclusion.
check the code.
Determine the types deduced in each of the following definitions. Once you’ve figured out the types, write a program to see whether you were correct.
const int i = 42; auto j = i; const auto &k = i; auto *p = &i; const auto j2 = i, &k2 = i;
i
isconst int
.j
isint
.k
isconst int&
.p
isconst int *
.j2
isconst int
.k2
isconst int&
.
check Here.
In the following code, determine the type of each variable and the value each variable has when the code finishes:
int a = 3, b = 4; decltype(a) c = a; decltype((b)) d = a; ++c; ++d;
c
is an int, d
is a reference of a
. all their value are 4
.
Assignment is an example of an expression that yields a reference type. The type is a reference to the type of the left-hand operand. That is, if i is an int, then the type of the expression
i = x
isint&
. Using that knowledge, determine the type and value of each variable in this code:int a = 3, b = 4; decltype(a) c = a; decltype(a = b) d = a;
c
is an int,d
is a reference of int.- the value:
a = 3, b = 4, c = 3, d = 3
Describe the differences in type deduction between
decltype
and auto. Give an example of an expression where auto anddecltype
will deduce the same type and an example where they will deduce differing types.
The way decltype
handles top-level const
and references differs subtly from the way auto
does. Another important difference between decltype
and auto
is that the deduction done by decltype
depends on the form of its given expression.
so the key of difference is subtly and form.
int i = 0, &r = i;
// same
auto a = i;
decltype(i) b = i;
// different
auto c = r;
decltype(r) d = i;
Compile the following program to see what happens when you forget the semicolon after a class definition. Remember the message for future reference.
struct Foo { /* empty */ } // Note: no semicolon int main() { return 0; }
Error message: [Error] expected ';' after struct definition
Write your own version of the
Sales_data
class.
struct Sale_data {
std::string bookNo;
std::string bookName;
unsigned units_sold = 0;
double revenue = 0.0;
double price = 0.0;
//...
};
Use your Sales_data class to rewrite the exercises in 1.5.1(p. 22), 1.5.2(p. 24), and 1.6(p. 25). For now, you should define your Sales_data class in the same file as your main function.
1.5.1
#include <iostream>
#include <string>
struct Sale_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sale_data book;
double price;
std::cin >> book.bookNo >> book.units_sold >> price;
book.revenue = book.units_sold * price;
std::cout << book.bookNo << " " << book.units_sold << " " << book.revenue << " " << price;
return 0;
}
1.5.2
#include <iostream>
#include <string>
struct Sale_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sale_data book1, book2;
double price1, price2;
std::cin >> book1.bookNo >> book1.units_sold >> price1;
std::cin >> book2.bookNo >> book2.units_sold >> price2;
book1.revenue = book1.units_sold * price1;
book2.revenue = book2.units_sold * price2;
if (book1.bookNo == book2.bookNo) {
unsigned totalCnt = book1.units_sold + book2.units_sold;
double totalRevenue = book1.revenue + book2.revenue;
std::cout << book1.bookNo << " " << totalCnt << " " << totalRevenue << " ";
if (totalCnt != 0)
std::cout << totalRevenue / totalCnt << std::endl;
else
std::cout << "(no sales)" << std::endl;
return 0;
}
else {
std::cerr << "Data must refer to same ISBN" << std::endl;
return -1; // indicate failure
}
}
1.6
#include <iostream>
#include <string>
struct Sale_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sale_data total;
double totalPrice;
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice) {
total.revenue = total.units_sold * totalPrice;
Sale_data trans;
double transPrice;
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice) {
trans.revenue = trans.units_sold * transPrice;
if (total.bookNo == trans.bookNo) {
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
else {
std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
std::cout << total.revenue / total.units_sold << std::endl;
else
std::cout << "(no sales)" << std::endl;
total.bookNo = trans.bookNo;
total.units_sold = trans.units_sold;
total.revenue = trans.revenue;
}
}
std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
std::cout << total.revenue / total.units_sold << std::endl;
else
std::cout << "(no sales)" << std::endl;
return 0;
}
else {
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
Write your own version of the
Sales_data.h
header and use it to rewrite the exercise from 2.6.2(p. 76)