TheCaptain
TheCaptain

Reputation: 135

mac terminal tab size is inconsistent

I am reading 'The C language'. In this exercise, I am supposed to write a program that replaces a tab with equivalent spaces. here is my code,

#include <stdio.h>

#define TABWIDTH 8

int main() {
    int c;

    while ((c = getchar()) != EOF) {
        if (c == '\t') {
            for (int i = 1; i <= TABWIDTH; ++i) {
                putchar(' ');
                //printf("%d\n", i);
            }
        } else {
            putchar(c);
        }
    }

    return 0;
}

The problem is that size of the tab is kinda inconsistent in the mac terminal. In my program, I assume it always going to be 8 spaces but in the terminal is not always 8. here is the output I get tab comes first

        k // this is tabed
        k // this is 8 spaces

tab comes after a character

k        k // this tabed
k          k // this 8 spaces

Upvotes: 0

Views: 319

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54563

The tab goes to the next tab-stop, which would be on columns 1,9,17,25,etc. In your example, you have a k preceding the tab, but that isn't counted (because the tab goes to column 9), while the k before a space is counted (because spacing ignores tab-stops).

Upvotes: 1

Related Questions