I desire to carry out a text illustration function. But I am no sure just how \t works, which means I don"t recognize how countless spaces I must print for \t.

You are watching: How many spaces does tab indent

I have actually come up through the adhering to algorithm:

a) every \t to represent at many NUMBER_OF_SPACES_FOR_TAB spaces.b) If \t shows up in the last line at a equivalent position, \t for this line should be aligned come the \t of critical line.

Example:

printf("a\t\tb\n");printf("\t\tc\n");Should print:

a11112222b34444cWhere:

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

2.NUMBER_OF_SPACES_FOR_TAB == 4

Does anyone understand the traditional algorithm? many thanks in advance.


*

A tab character should development to the following tab stop. Historically tab stops to be every 8th character, although smaller values room in usual use today and most editors can be configured.

I would intend your calculation to look like the following:

123456789a b cThe algorithm is to begin a tower count in ~ zero, then increment it because that each character output. As soon as you obtain to a tab, output n-(c%n) spaces wherein c is the tower number (zero based) and n is the tab spacing.


*

Imagine a leader with tab stops every 8 spaces. A tab character will align message to the following tab stop.

0 8 16 24 32 40 |.......|.......|.......|.......|.......|printf("\tbar\n"); \t barprintf("foo\tbar\n"); foo\t barprintf("longerfoo\tbar"); longerfoo\t barTo calculate wherein the next tab protect against is, take the current column.

nextTabStop = (column + 8) / 8 * 8The / 8 * 8 part effectively truncates the result to the nearest multiple of 8. For example, if you"re at shaft 11, climate (11 + 8) is 19 and 19 / 8 is 2, and also 2 * 8 is 16. For this reason the following tab prevent from obelisk 11 is at column 16.

In a message editor you may configure tab stop to smaller sized intervals, favor every 4 spaces. If you"re simulating what tabs look prefer at a terminal you need to stick through 8 spaces every tab.


*

A Tab character shifts end to the next tab stop. By default, there is one every 8 spaces. But in most shells friend can quickly edit that to it is in whatever variety of spaces you desire (profile choices in linux, set tabstop in vim).


*

Thanks for contributing an answer to stack Overflow!

Please be certain to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to various other answers.Making statements based on opinion; ago them increase with referrals or personal experience.

See more: Vitamins Chem Is Vitamin C Polar Or Nonpolar, Nutrient Solubility

To find out more, see our tips on writing great answers.


write-up Your prize Discard

By click “Post your Answer”, you agree come our terms of service, privacy policy and cookie plan


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


replacing a 32-bit loop respond to with 64-bit introduces crazy performance deviations through _mm_popcnt_u64 on Intel CPUs
*

site style / logo design © 2021 stack Exchange Inc; user contributions licensed under cc by-sa. Rev2021.10.1.40358


your privacy

By click “Accept all cookies”, girlfriend agree ridge Exchange deserve to store cookies on your machine and disclose information in accordance through our Cookie Policy.