How many spaces is a tab in C++

I want to implement a text drawing function. But I am not sure how \t works, which means I don't know how many spaces I should print for \t.

I have come up with the following algorithm:

a) Each \t represents at most NUMBER_OF_SPACES_FOR_TAB spaces. b) If \t appears in the last line at a corresponding position, \t for this line should be aligned to the \t of last line.

Example:

printf("a\t\tb\n");
printf("\t\tc\n");

Should print:

a11112222b
34444c

Where:

1.Number i represents the spaces of \t at position i

2.NUMBER_OF_SPACES_FOR_TAB == 4

Does anyone know the standard algorithm? Thanks in advance.

asked Oct 26, 2012 at 21:28

Chao ZhangChao Zhang

1,4162 gold badges14 silver badges25 bronze badges

5

A tab character should advance to the next tab stop. Historically tab stops were every 8th character, although smaller values are in common use today and most editors can be configured.

I would expect your output to look like the following:

123456789
a       b
        c

The algorithm is to start a column count at zero, then increment it for each character output. When you get to a tab, output n-(c%n) spaces where c is the column number (zero based) and n is the tab spacing.

answered Oct 26, 2012 at 21:32

Mark RansomMark Ransom

290k40 gold badges384 silver badges607 bronze badges

16

Imagine a ruler with tab stops every 8 spaces. A tab character will align text to the next tab stop.

                                0       8       16      24      32      40
                                |.......|.......|.......|.......|.......|
printf("\tbar\n");              \t      bar
printf("foo\tbar\n");           foo\t   bar
printf("longerfoo\tbar");       longerfoo\t     bar

To calculate where the next tab stop is, take the current column.

nextTabStop = (column + 8) / 8 * 8

The / 8 * 8 part effectively truncates the result to the nearest multiple of 8. For example, if you're at column 11, then (11 + 8) is 19 and 19 / 8 is 2, and 2 * 8 is 16. So the next tab stop from column 11 is at column 16.

In a text editor you may configure tab stops to smaller intervals, like every 4 spaces. If you're simulating what tabs look like at a terminal you should stick with 8 spaces per tab.

answered Oct 26, 2012 at 21:33

John KugelmanJohn Kugelman

336k66 gold badges509 silver badges559 bronze badges

3

A Tab character shifts over to the next tab stop. By default, there is one every 8 spaces. But in most shells you can easily edit it to be whatever number of spaces you want (profile preferences in linux, set tabstop in vim).

answered Oct 26, 2012 at 21:35

WhyrusleepingWhyrusleeping

8392 gold badges8 silver badges20 bronze badges

This doesn't necessarily apply to the article, but In the old days of typewriters and fixed-width fonts, a tab was 5 spaces, because five characters was half an inch. That's pretty irrelevant now, though.

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

In http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html says that tabs must be 8 spaces. But I remember that tabs is 4 spaces from one training course. I am confused.
Is it right that indent will be 4 spaces and tabs will be 8 spaces? But in which condition will use tabs or indent?
Thanks!

Rome was not built in a day.

Sheriff

How many spaces is a tab in C++

Posts: 9109

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

Generally, you indent code 4 spaces e.g.

but if the line is too long (more than 80 characters), you indent it 8 spaces (tab)

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Mikey Chen

Greenhorn

Posts: 29

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

Thanks!

Rome was not built in a day.

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

Tabs are to be avoided at all costs. Since different editors treat tabs differently. Always use spaces to indent your code. (If you are just working my yourself this isn't as important.)
And there is no universal standand for how many spaces to indent. But it should be agreed upon if you are working with a group of people.

Please ignore post, I have no idea what I am talking about.

mister krabs

Posts: 13974

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

Many editors have an option to insert spaces when using the tab key. You should always have that turned on.

author

How many spaces is a tab in C++

Posts: 9046

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

Sun sez: each indent should be four spaces from the last level, and tabs should be eight spaces. So your first indent has to be four spaces, your second indent can be either eight spaces or a tab. The third can be 12 spaces or a tab and four spaces, and so on...
So, everyone has a different style, but this is Sun's recommended style - and if you ever take the developer's exam it's the one you better use, even if your company uses a different style. Same is true for things like where to put your curly braces.
Don't shoot me I'm just the piano player.

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

In the old and moldy days, all tabs were 8 spaces. Then some editors said you could specify how many spaces equaled a tab. So many geeks went with "4", that a lot of editors started making that the default. In the mean time, lots of programs stubbornly insist on 8!
So then some poor slob gets a source file where the author has his stuff set up for, say, 4. Half the line are moved with tabs and half with spaces. If the slob in question has all of his stuff fixed at 4 spaces equals a tab, everything is cool! But if he has stuff set for 8, everything comes out all screwy and hard to read.
My impression is that everybody is slowly moving back to a tab being the original 8 spaces and people aren't using tabs like that anymore. They use strictly the space character so nobody will see any goofiness.

posted 19 years ago

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

    Number of slices to send:

    Optional 'thank-you' note:

  • How many spaces is a tab in C++
  • How many spaces is a tab in C++

Indents are not Tabs and Tabs are not Indents.
Indents in code should be done using spaces. Some style guides say 4, some 2, and I even know some who insist on 3. But the "implementation of an indent" uses some number of the SPACE character (ASCII 0x20 or "\u0020" or just " ").
A Tab is a single character (known as "HT" or ASCII 0x09 or "\u0009" or "\t"). Often when that character is displayed, it is rendered as some ammount of blank area. It has traditionally been rendered as the equivalent of 8 SPACE characters. More often, it is rendered as "enough SPACE characters to reach the next column that is a multiple of 8 (or N) spaces". And sometimes it is some sort of arrow-like character.
Because of the ambiguity in rendering, these Tab characters are worthless for code indention.
However, there is a nice big key labeled "Tab" on your keyboard. It would be a shame to let a nice key like that go to waste, especially since it is easier to type than lots of spaces. For this reason, most code editors interpret your pressing of the Tab key as some sort of "Indent" signal. Good editors will take that tab and convert it into the correct number of initial spaces to make a proper indent.
PS -
Note that a Tab can also be used as a data delimiter (as in something to go between columns of a spreadsheet-to-text output or something.
PPS -
Tab is also a diet drink marketed by the Coca-Cola company. You might risk copyright/patent violations if you release code containing lots of Tab.
US$ 0.02,
Dave

How many spaces is a tab in C++

Is a tab 4 or 8 spaces?

The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

Is tab always 4 spaces?

Sets the distance in spaces between tab stops. The default is four spaces.

How many spaces are there in tab?

Indentation: tabs vs spaces Java: 4 spaces, tabs must be set at 8 spaces.

How much space a tab key is?

Common default tab widths are four spaces (in a monospaced text document), or half an inch (in a word processor). If formatting marks are enabled in your document editor, the tab character is often displayed as an arrow. The Tab key is located to the left of the Q key and above the Caps Lock key on keyboards.