//float 误差
int main() {
//OUT();
float a = 1.f / 81;
float b = 0;
for (int i = 0; i < 729; ++i) b += a;
printf("%.7g\n", b);
double c = 1.0 / 81;
double d = 0;
for (int i = 0; i < 729; ++i) d += c;
printf("%.15g\n", d);
//b = 9.000023
//d = 8.99999999999996
return 0;
}
//无符号与有符号类型的转换
int main() {
using UI = unsigned;
unsigned u = 10;
int i = -42;
cout << i + i << endl;
cout << u + i << endl;
cout << (UI)(1 << 32) - 42 + 10 << endl;
cout << (UI)(-1) << endl;
cout << (UI)(1 << 32)-1 << endl;
//负数的存储,首位存符号位,剩下的存大小,然后取反加一以补码的形式存储,int太长了,我用char做示范吧
signed char a = -1;
unsigned char b = a;
printf("%d\n", b);// : 255
//原码为1000 0000 0000 0001 反码为 1111 1111 1111 1111
return 0;
}
int main() {
using UI = unsigned;
for (int i = 10; i >= 0; --i) cout << i << endl;
//never terminate
for (UI u = 10; u >= 0; --u) cout << u << endl;
return 0;
}
//Exercises Section 2.1.2
//Exercise 2.3: What output will the following code produce ?
int main() {
using UI = unsigned int;
unsigned u = 10, u2 = 42;
cout << u2 - u << endl; //32
cout << u - u2 << endl; cout << (UI)(1 << 32) - 32 << endl;
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
return 0;
}
//Exercises Section 2.1.3
//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
//Exercise 2.6: What, if any, are the differences between the following
//definitions :
//int month = 9, day = 7;
//int month = 09, day = 07; //error 09 is meaningless octal digit
//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
//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.
int main() {
int month = 9, day = 7;
//int month = 09, day = 017;
cout << "who goes with F\145rgus?\012" << endl;
cout << '\012' << '\145';
cout << 3.14e1L << endl;
cout << 1034.f << endl;
cout << 3.14L << endl;
cout << 'a' << L'a' << "a" << L"a" << endl;
cout << 10 << 10u << 10L << 10uL << 010 << 0xC;
cout << 3.14 << 3.14f << 3.14L << endl;
cout << 10 << 10u << 10. << 10e-2 << endl;
cout << "\062\155\12" << endl;
cout << "\062\t\155\n" << endl;
return 0;
}
//Exercises Section 2.2.1
//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;
//Exercise 2.10: What are the initial values, if any, of each of the following
//variables ?
std::string global_str;//0
int global_int;//0
int main()
{
//int i = { 3.14 };//error : conversion from 'double' to 'int' requires a narrowing conversion
int local_int;//uninitialized
//cout << local_int << endl;
std::string local_str;//""
}
//Exercises Section 2.2.2
//Exercise 2.11: Explain whether each of the following is a declaration or a
//definition :
//(a) extern int ix = 1024;//defination
//(b) int iy;//defination
//(c) extern int iz;// 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;
int _a;
int not;
int main() {
int _ = 1;
int _1or_2 = 1;
int _A = 1;
int __ = 1;
return 0;
}
//Exercises Section 2.3.1
//Exercise 2.15: Which of the following definitions, if any, are invalid ? Why ?
//(a) int ival = 1.01;
//(b) int &rval1 = 1.01;//invalid
//(c) int &rval2 = ival;
//(d) int &rval3;//invalid
//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;
//all is valid
//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
//Exercises Section 2.3.2
//Exercise 2.18: Write code to change the value of a pointer.Write code to
//change the value to which the pointer points.
//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 ?
//int i = 42;
//int *p1 = &i;
//*p1 = *p1 * *p1;
//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;//illegal, the type should be reponded.
//(b) int *ip = i;
//(c) int *p = &i;
//Exercise 2.22: Assuming p is a pointer to int, explain the following code :
//if (p) // ... //this means when p != nullptr;
//if (*p) // ... //this means when i != 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?
//Exercise 2.24 : Why is the initialization of p legal but that of lp illegal ?
//int i = 42;
//void *p = &i;
//long *lp = &i;
//Ingerited from C, void* is a special pointer that may point to any type.
//C++forbids implicit conversions.
//Exercises Section 2.4.4
//Exercise 2.32: Is the following code legal or not? If not, how might you
//make it legal ?
//int null = 0, *p = null;
int main() {
// cannot convert from 'int' to 'int *'
//int null = 0, *p = null;
int null = 0, *p = 0;
int null2 = 0, *p = nullptr;
return 0;
}
Chapter 2. Variables and Basic Types
最新推荐文章于 2024-09-25 10:19:07 发布