Exercises and Solutions of Chapter 0

  • Exercise 0-0——Understanding the “Hello World” program in depth
  • Exercise 0-1——Understanding expressions, results, and side effects
  • Exercise 0-2——Practice special string literal treatments
  • Exercise 0-3——Practice special string literal treatments
  • Exercise 0-4——Write a program that create “Hello World” program
  • Exercise 0-5——Understanding scope
  • Exercise 0-6——Understanding scope
  • Exercise 0-7——Understanding comment usage
  • Exercise 0-8——Understanding comment usage
  • Exercise 0-9——Create the shortest valid program
  • Exercise 0-10——Practice special string literal treatment

0-0 Compile and run the Hello, world! program.

Solution:

// Hello World! Program
#include <iostream>
int main()
{
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

You have to build it using a compiler you are using (for example g++).

0-1 What does the following statement do?

3 + 4;

Solution:

This statement yields an int (integer) with a value of 7. Note that this value is not saved anywhere.

0-2 Write a program that, when run, writes “This (") is a quote, and this (\) is a backslash.”

Solution:

The trick here is using escape characters such as \" and \\ to print characters that might otherwise break the string.

#include <iostream>
int main()
{
    std::cout << "This (\") is a quote, and this (\\) is a backslash." << std::endl;
    return 0;
}

0-3 The string literal "\t" represents a tab character; different C++ implementations display tabs in different ways. Experiment with your implementation to learn how it treats tabs.

Solution:

Four spaces are used instead of tabs to make the output similar to the original code written in VSCode. Yet it seems g++ uses 8 spaces for a tab (in other word, a tab aligns 8 characters in length). In the second line, you will see that the output has 8 spaces before the dot.

#include <iostream>
int main()
{
    std::cout << "A single tab is \n(\t)." << std::endl;
    std::cout << "Seven spaces are \n(       )." << std::endl;
    return 0;
}

0-4 Write a program that, when run, writes the Hello, world! program as its output.

Solution:

#include <iostream>
int main()
{
    std::cout
        << "// Hello World program" << std::endl
        << "#include <iostream>" << std::endl
        << "int main()" << std::endl
        << "{" << std::endl
        << "    std::cout << \"Hello, world!\" << std::endl;" << std::endl
        << "    return 0;" << std::endl
        << "}";
    return 0;
}

0-5 Is this a valid program? Why or why not?

#include <iostream>

int main() std::cout << "Hello, world!" << std::endl;

Solution:

This is not a valid program as the curly braces which should be present after the main function are missing. It will get an compilation error regarding this issue.

0-6 Is this a valid program? Why or why not?

#include <iostream>
int main() {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}}

Solution:

This is a valid program, as the curly brackets can be stacked indefinitely. One pair of curly brackets was enough but more can be used for other matters such as limiting the scope of a variable. The unnecessary brackets were probably ignored by the compiler.

0-7 What about this one?

#include <iostream>
int main()
{
    /* This is a comment that extends over several lines
    because it uses /* and */ as its starting and ending delimiters */
    std::cout << "Does this work?" << std::endl;
    return 0;
}

Solution: 

This is not a valid program, as the first */ in the comment closes the multi-line comment. The words after that remain outside the comment block, causing errors.

0-8 And this one?

#include <iostream>
int main()
{
 // This is a comment that extends over several lines
 // by using // at the beginning of each line instead of using /*
 // or */ to delimit comments.
 std::cout << "Does this work?" << std::endl;
 return 0;
}

Solution: 

This is a valid program, as the multi-line comment uses // at the beginning of each line.

0-9 What is the shortest valid program?

Solution:

The shortest valid program is

main(){}

0-10 Rewrite the Hello, world! program so that a newline occurs everywhere that whitespace is allowed in the program.

Solution:

// Hello World program
#include <iostream>
int
main
(
)
{
std
::
cout
<<
"Hello, world!"
<<
std
::
endl
;
return
0
;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Snivellus Snape

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值