printf() \t option

When you are printing a tab character to the standard output using printf in C, it outputs some space which is apparently 4 characters in length.

printf("\t");

Is there a way by which I can control the tab width in the above case?


That's something controlled by your terminal, not by printf.

printf simply sends a \t to the output stream (which can be a tty, a file, etc.). It doesn't send a number of spaces.


A tab is a tab. How many spaces it consumes is a display issue, and depends on the settings of your shell.

If you want to control the width of your data, then you could use the width sub-specifiers in the printf format string. For example,

printf("%5d", 2);

It's not a complete solution (if the value is longer than five characters, it will not be truncated), but it might be OK for your needs.

If you want complete control, you'll probably have to implement it yourself.