Solutions to exercises in Chapter 0 of Accelerated C++, “Getting started.”
Exercise 0-0
Compile and run the Hello, world!
program.
Hint
Make sure that you locate the documentation and help files for your compiler and editing environment.
Solution
Consult the documentation for your particular compiler. Each will have its own set of directives. For the GNU C++ compiler, assuming the code is in a file named hello.cpp
, the following may be entered at the command line:
1 | g++ hello.cpp |
This will produce a file named a.out
, or on Windows systems a.exe
, that can then be executed like any other program.
Here is the source for the Hello, world!
program.
1 2 3 4 5 6 7 | #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; } |
Exercise 0-1
What does the following statement do?
1 | 3 + 4; |
Hint
This statement is an expression. Consider what may happen when an expression is evaluated, and what the result of the expression might me.
Solution
This statement is an expression that yields the result 7
and has no side effects.
Exercise 0-2
Write a program that, when run, writes
1 | This (") is a quote, and this (\) is a backslash. |
Hint
The backslash character has a special meaning in a string literal.
Solution
The quote and backslash characters must be preceded by a backslash, which is the escape character.
1 2 3 4 5 6 7 8 9 10 | #include <iostream> int main() { std::cout << "This (\") is a quote, and this (\\) is a backslash." << std::endl; return 0; } |
Exercise 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
For now, self-directed study. More information coming.
Exercise 0-4
Write a program that, when run, writes the Hello, world!
program as its output.
Solution
The main issue here is properly escaping special characters such as quotes, and adding newline (\n
)
characters where appropriate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> int main() { std::cout << "#include <iostream>\n" << "\n" << "int main()\n" << "{\n" << " std::cout << \"Hello, world!\" << std::endl;\n" << " return 0;\n" << "}\n"; return 0; } |
thank you for this good book.
Where is the next page?
I started reading the book and I want to solve 0-10 exercise. However, I don’t really understand the problem. I don’t want to be spoonfed the solution so can someone just elaborate more what is required of me?
“0-10. Rewrite the Hello, world! program so that a newline occurs everywhere that whitespace is allowed in the program.”
“C++ programs are usually in free form, meaning that spaces are required only when they keep adjacent symbols from running together. In particular, newlines are just another kind of space.”
In most cases, we can add one or more whitespaces between two adjacent symbols, such as “std” and “::”, and the whitespaces can be replaced by a newline.
The exercise ask you to start a newline everywhere you can.