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
Answers:
(a) ‘a’, char L’a’, long char “a”, string L”a” long string
(b) 10, integer 10u, unsigned integer 10L, long integer 10uL, unsigned long integer 012, octal integer 0xC hexadecimal integer
(c) 3.14, double 3.14f, float 3.14L long double
(d) 10, integer 10u, unsigned integer 10., double 10e-2 double
Exercise 2.6: What, if any, are the differences between the following
definitions:
int month = 9, day = 7;
int month = 09, day = 07;
Answers:
int month = 9, day = 7; decimal integer
int month = 09, day = 07; octal integer
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
Answers:
(a) “Who goes with F\145rgus?\012” : value: Who goes with Fergus? type: string
(b) 3.14e1L value: 34.1 type: long double
(c) 1024f building error, should 1024.0f value: 1024 type: float
(d) 3.14L value : 3.14 type: 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.
Answers:
1)
#include <iostream>
int main()
{
std::cout<< "2\x4d\n" <<std::endl;
return 0;
}
2)
#include <iostream>
int main()
{
std::cout<< "2\t\x4d\n" <<std::endl;
return 0;
}