1、练习题
1.3
# cat 1-3.cc
/*
* File : 1-3.cc
* Author : ZhangLe
* CreateTime : 2023-04-14 21:12:08
* LastModified : 2023-04-14 21:12:48
* Vim : ts=4, sw=4
*/
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
return 0;
}
1.4
cat 1-4.cc
/*
* File : 1-4.cc
* Author : ZhangLe
* CreateTime : 2023-04-14 21:13:36
* LastModified : 2023-04-14 21:19:04
* Vim : ts=4, sw=4
*/
#include <iostream>
using namespace std;
int main() {
cout << " Enter two numbers: " << endl;
int v1 = 0, v2 = 0;
cin >> v1 >> v2;
cout << " The product of " << v1 << " and " << v2
<< " is " << v1 * v2 << endl;
return 0;
}
1.5
cat 1-5.cc
/*
* File : 1-4.cc
* Author : ZhangLe
* CreateTime : 2023-04-14 21:13:36
* LastModified : 2023-04-14 21:24:45
* Vim : ts=4, sw=4
*/
#include <iostream>
using namespace std;
int main() {
int v1 = 0, v2 = 0;
cout << " v1: ";
cin >> v1;
cout << " v2: ";
cin >> v2;
//cout << " v1: " << v1 << endl;
//cout << " v2: " << v2 << endl;
cout << " v1 * v2 = " << v1 * v2 << endl;
return 0;
}
1.6
编译报错。std::cout
后面不能在添加;
这里属于一个语句的结束。
cat 1-6.cc
/*
* File : 1-6.cc
* Author : ZhangLe
* CreateTime : 2023-04-14 21:25:36
* LastModified : 2023-04-14 21:27:15
* Vim : ts=4, sw=4
*/
#include <iostream>
using namespace std;
int main() {
int v1 = 3, v2 = 4;
cout << "the sum of" << v1
<< " and " << v2
<< " is " << v1 + v2 << endl;
return 0;
}
1.7
cat 1-7.cc
/*
* File : 1-7.cc
* Author : ZhangLe
* CreateTime : 2023-04-14 21:40:14
* LastModified : 2023-04-14 21:41:19
* Vim : ts=4, sw=4
*/
#include <iostream>
using namespace std;
int main() {
/*
hello world
/* */ XXXXXX
*/
return 0;
}
[root@192 1]# g++ 1-7.cc
1-7.cc: In function ‘int main()’:
1-7.cc:16:14: error: ‘XXXXXX’ was not declared in this scope <==== 报错在此范围内没有声明
/* */ XXXXXX
^
1-7.cc:17:6: error: expected primary-expression before ‘/’ token <==== 报错在此范围前期待一个主表达式
*/
^
1-7.cc:18:5: error: expected primary-expression before ‘return’
return 0;
^
1-7.cc:18:5: error: expected ‘;’ before ‘return’
1.8
[root@192 1]# cat 1-8.cc
/*
* File : 1-8.cc
* Author : ZhangLe
* CreateTime : 2023-04-14 21:43:54
* LastModified : 2023-04-14 21:49:48
* Vim : ts=4, sw=4
*/
#include <iostream>
using namespace std;
int main() {
cout << "/*"; // ok
cout << "*/"; // ok
// cout << /* "*/" */; // no
cout << /**/ "*/" /**/; // ok
cout << /* "*/" /*"/*" */; // ok
return 0;
}
[root@192 1]# g++ 1-8.cc