How many space is a tab

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

289k40 gold badges382 silver badges606 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

335k66 gold badges508 silver badges558 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

Not the answer you're looking for? Browse other questions tagged c++ c printf or ask your own question.

Is a tab 4 or 8 spaces?

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

How many spaces is the tab button?

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.

Is tab equal to 2 spaces?

Answer. In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each.

How big is a tab space?

The default value for the tab-size property is 8 space characters, and it can accept any positive integer value.