Accelerated C++ Solution to Exercise 0-3

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.

Experiments

I shall be running a number of experiment C++ programs to test out the tab behaviour. I am running this using the Code::Block IDE (Integrated Development Environment) on Window Vista platform. (According to a number of online forums the tab property differs depending on the platform (e.g. tab length).

Experiment 1

In this experiment I will add various number of tabs at the start of a line.


<h1>include <iostream></h1>

<p>int main()
{
    std::cout << "Start the following line with tabs" << std::endl;
    std::cout << "\tStart this line with 1 tab." << std::endl;
    std::cout << "\t\tStart this line with 2 tabs." << std::endl;
    std::cout << "\t\t\tStart this line with 3 tabs." << std::endl;
    return 0;
}

Result (experiment 1)

Submitting the program produces the following in the console output window.

Start the following line with tabs
         Start this line with 1 tab.
                 Start this line with 2 tabs.
                         Start this line with 3 tabs.

Process returned 0 (0x0)   execution time : 0.235 s
Press any key to continue.

Observations:

  • Should the line begins with 1 tab, the tab length is 8 spaces.
  • With 2 (consecutive) tabs, the second tab adds 8 spaces. i.e. total indentation is 8 + 8 = 16 spaces.
  • With 3 (consecutive) tabs, the third tab is adds spaces. i.e. total indentation is 8 + 8 + 8 = 24 spaces.

From this it appears that each tab corresponds to 8 spaces.

Experiment 2

In this experiment I will add various number of tabs within a line.

#include <iostream>
int main()
{
    std::cout << "Add tabs within the line" << std::endl;
    std::cout << "I will add 1 tabs\twithin this line line." << std::endl;
    std::cout << "I will add 2 tabs\t\twithin this line." << std::endl;
    std::cout << "I will add 3 tabs\t\t\twithin this line." << std::endl;
    return 0;
}

Result

Submitting the program produces the following in the console output window.

Add tabs within the line
I will add 1 tabs       within this line line.
I will add 2 tabs               within this line.
I will add 3 tabs                       within this line.

Process returned 0 (0x0)   execution time : 0.159 s
Press any key to continue.

Observations:

  • Should the line contains 1 tab, the tab length is 7 spaces.
  • With 2 (consecutive) tabs, the second tab adds 8 spaces. i.e. total tab length is 7 + 8 = 15 spaces.
  • With 3 (consecutive) tabs, the third tab is 8 spaces. i.e. total indentation is 7 + 8 + 8 = 23 spaces.

From this it appears that each tab is of 8 spaces, except the first tab which is of 7. (I’m not entirely sure why though.)

Conclusion

Merging experiment 1 and 2, it appears that each tab in general takes up a fix length of 8 spaces. The only exception is the first tab that is placed within a line (as seen in experiment 2), which takes up only 7 spaces (i.e. 1 space less). I am not entirely sure why at this stage.

This reminds of a message that the authors mention in chapter 0:

… string literal, … a mysterious type that we shall not even discuss until section 10.2 / page 176…

So is it something that is hidden that require deeper understanding?I hope to find out more in later chapter(s).

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

One thought on “Accelerated C++ Solution to Exercise 0-3”

Comments are closed.