C++ Primer 第一章

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值