How many spaces for tab character(\t)?

115,320

Solution 1

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.

Solution 2

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.

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

Share:
115,320
Chao Zhang
Author by

Chao Zhang

Updated on April 24, 2020

Comments

  • Chao Zhang
    Chao Zhang about 4 years

    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.

  • Chao Zhang
    Chao Zhang over 11 years
    What about the algorithm I mentiond?
  • rekire
    rekire over 11 years
    Often are 4 spaces used today. For languages with many levels also 2 are not uncommon.
  • Admin
    Admin over 11 years
    @rekire and Mr Torvalds still advises using 8, and he has a pretty good reason :)
  • Admin
    Admin over 11 years
    @Grizzly do you know what his reasoning is?
  • Chao Zhang
    Chao Zhang over 11 years
    @Mark Ransom as you describes, the tab after character 'a' would just be 7 spaces(which I think it would be better if it is 8)?
  • Chao Zhang
    Chao Zhang over 11 years
    The tab after foo represents 5 spaces? I would assume it would be better if is 8 spaces.
  • Mark Ransom
    Mark Ransom over 11 years
    @ComboZhc, no 7 spaces is the correct answer. The 'a' takes one space so it only takes 7 to get to stop.
  • Damon
    Damon over 11 years
    @H2CO3: Reason of Mr. Torvalds is disputable if you read any of the stuff he regularly posts on the internet... regardless, anyone recommending spaces for tabs doesn't have much understanding of what tabs are. Tabs are not spaces. One tab indents by one level, that's not 1 space, nor 2, nor 4, nor 8, nor any other number. Common excuses range from "but it looks like this" via "we've always done that" to "some people I have to work with use editors that can't do tabs", but none of the excuses is truly valid.
  • Damon
    Damon over 11 years
    Or, worded differently, claiming that a tab is 8 spaces is like claiming that å is the same as a, or a dash, a hyphen, and a minus sign are the same thing, И is the russian N, and Я is the russian R.
  • Abhineet Sharma
    Abhineet Sharma almost 9 years
    Is 'column' understood by standard library? The math of tabs and spaces is clear, but is it not based on reference of 'columns'?
  • John Kugelman
    John Kugelman almost 9 years
    column is a hypothetical variable that you need to keep track of yourself. The standard library does not know the current column position of the cursor, no.
  • Lightness Races in Orbit
    Lightness Races in Orbit over 8 years
    @Damon: Oh thank god. Someone sane. Most people I run into nowadays swear blind that tab indentation is evil. They can never provide a good explanation as to why. It's starting to drive me insane!
  • cmaster - reinstate monica
    cmaster - reinstate monica about 4 years
    @LightnessRacesinOrbit Full Ack. And the code editors of those people are even worse. I've seen code indented with three spaces, but whenever more than eight spaces accumulated, they were replaced by a tab. It almost hurts physically to look at the resulting mess with a tab setting of four, the third level would appear further to the left than the second level! Completely unreadable until you've reindented the whole thing.
  • Mark Ransom
    Mark Ransom about 4 years
    @cmaster that's why I prefer to use editors that replace tabs with spaces. Tabs can be ambiguous, spaces never are.
  • cmaster - reinstate monica
    cmaster - reinstate monica about 4 years
    @MarkRansom Tabs are not ambiguous. They carry meaning. One tab = one indentation level. How wide I want to display that indentation level, is my choice. I don't want to be forced by highly influential code developers to view my code with an indentation width of 8. If that code is properly formatted with tabs, I can view it with an indentation of, say, three, while Mr. Torvalds can view the same code with eight. My editor, my choice how it's displayed. The problems start when someone, or some tool, thinks they can replace a tab with eight spaces. That's just not the meaning of a tab.
  • Mark Ransom
    Mark Ransom about 4 years
    @cmaster you're demonstrating the problem right there. If two people with different tab configurations view the same file, they'll get different representations. There are bound to be places that don't look right because things don't line up anymore.
  • cmaster - reinstate monica
    cmaster - reinstate monica about 4 years
    @MarkRansom No, there are not. If the tabs are used for their designed purpose only, there are no such places: Indentation stays indentation, and alignment needs to be done with spaces, not tabs. Really, I'm using tab indentation everywhere where I have the choice, and the code that I produce can be viewed correctly with any tab setting. The only key to achieving this is to use tabs for indentation only, and spaces for alignment only. The two characters carry different meaning, and they need to be used as such.
  • Mark Ransom
    Mark Ransom about 4 years
    @cmaster once you get used to using tabs, it's very difficult to switch between tabs and spaces as necessary. You will start using tabs for alignment. My recommendation to use an editor that converts tabs to spaces is based on my own experience, not theory - it's the way our entire team works, and it works very well for us. But we've also standardized our indentation levels, which helps a lot too.
  • cmaster - reinstate monica
    cmaster - reinstate monica about 4 years
    @MarkRansom Well, I'm programming for 25 years now, always indenting with tabs. And no, I never use tabs for alignment. Some editors do such brain-dead things, which were written by people who never meditated a second on the difference between a tab and a space. But even those editors can be taught not to do such nasty stuff. I mean, it's not that complicated. We are programming much, much more complex stuff in much, much more complex languages. You cannot tell me that we are not able to use tabs and spaces correctly...