I want to stop the \t tab after 5 spaces, How do i do that?

Dani AI

Generated

Short answer: the C language only emits the ASCII tab character (0x09). How many spaces that appears as is decided by the program that displays the output (terminal, pager, editor). To get "tabs every 5 spaces" you must either change the consumer's tab stops, convert tabs to spaces, or stop using \t and output fixed padding from your program.

Practical options, from easiest to most robust:

  • Change the viewer/pager/editor. Many pagers let you set tab stops (for example less -x5). Terminal emulators often have a tab-stop setting or a tabs utility. See the general behaviour of the tab character here: Tab character on Wikipedia.

  • Convert tabs to spaces after the program prints. On Unix-like systems expand -t 5 file > out will replace tabs with the number of spaces needed to hit 5-column stops; this is handy for files or pipelines. See expand documentation: expand(1) manual.

  • Change your program to produce spaces (recommended when you need exact layout). Replace \t at output time with the number of spaces needed to reach the next 5-column stop. The following routine does that:

#include <stdio.h>

void print_with_tabwidth(const char *s, int tabwidth) {
    int col = 0;
    for (; *s; ++s) {
        if (*s == '\t') {
            int spaces = tabwidth - (col % tabwidth);
            while (spaces--) { putchar(' '); ++col; }
        } else {
            putchar(*s);
            if (*s == '\n') col = 0; else ++col;
        }
    }
}

Notes tied to the thread: was correct that the display decides tab width. ’s pointer to terminal settings pointed in a useful direction but stty itself does not change visual tab stops. and offered quick hacks; they can work but are brittle compared with expanding tabs or formatting fields explicitly. If you changed a setting and saw no effect, confirm which program is rendering the output (terminal vs pager vs editor) and verify the output actually contains \t (use hexdump/od or pipe through expand) before changing code.

Recommended Answers

All 5 Replies

Not sure whether you can do it , but check stty manual.

You could try something like below...I generally don't like the solution but it works for some situations

#include <stdio.h>
#include <stdlib.h>

#define t "     "

int main(int argc, char**argv)
{		
	fprintf(stdout, "this\tis\ta\ttest", stdout);	
	exit(EXIT_SUCCESS);
}

You could try something like below...I

That should produce an error because \' ' is not a valid escape sequence.

If you don't like the default spacing then don't use tabs. Do something like this: printf("%-20s", "Hello World"); where the text string will be left-justified in a field 20 characters wide, right-side padded with spaces.

C doesn't specify tab stops, only tab characters. It's up to the program consuming those characters to interpret the character with a certain tab stop length, just like it's up to the same program to accurately print the 'Q' character. Translation: change the properties of whatever program is displaying those tabs to use a five character tab stop explicitly. Most likely this will be your shell, for command line programs that send output to stdout.

It does not work for me.

It is as the default. I modified it but it does not worked.

Help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.