Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many spaces for tab character(\t)?

Tags:

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.

like image 898
Chao Zhang Avatar asked Oct 26 '12 21:10

Chao Zhang


People also ask

How many spaces does \t do?

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.

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).

Is tab always 4 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.


1 Answers

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.

like image 199
Mark Ransom Avatar answered Oct 12 '22 14:10

Mark Ransom