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
;
}

猜你喜欢

转载自blog.csdn.net/ycy1300585044/article/details/132745113