Chapter 0 – Getting Started

Solutions to exercises in Chapter 0 of Accelerated C++, “Getting started.”

Exercise 0-0

Compile and run the Hello, world! program.

expand/collapse icon Hint

Make sure that you locate the documentation and help files for your compiler and editing environment.

expand/collapse icon 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:

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.

Exercise 0-1

What does the following statement do?

expand/collapse icon Hint

This statement is an expression. Consider what may happen when an expression is evaluated, and what the result of the expression might me.

expand/collapse icon 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

expand/collapse icon Hint

The backslash character has a special meaning in a string literal.

expand/collapse icon Solution

The quote and backslash characters must be preceded by a backslash, which is the escape character.

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.

expand/collapse icon 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.

expand/collapse icon Solution

The main issue here is properly escaping special characters such as quotes, and adding newline (\n)
characters where appropriate.

Share

3 Comments

  1. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *


*