Why is it that when I use tabulation on a printf, there are cases where it doesn’t tabulate correctly?

Asked

Viewed 77 times

3

I have this code:

#include <stdio.h>
void main(){
    int fahr, celsius;
    int inicio, fim, incr;

    inicio = 0;
    fim = 300;
    incr = 20;
    fahr = inicio;
    while(fahr <= fim){
        celsius = 5 * (fahr-32)/9;
        printf("Fahr: %d\tCelsius:%d\n", fahr, celsius);
        fahr = fahr + incr;
    }
}

The exit is being:

Fahr: 0 Celsius: -17
Fahr: 20    Celsius: -6
Fahr: 40    Celsius: 4
Fahr: 60    Celsius: 15
Fahr: 80    Celsius: 26
Fahr: 100   Celsius: 37
Fahr: 120   Celsius: 48
Fahr: 140   Celsius: 60
Fahr: 160   Celsius: 71
Fahr: 180   Celsius: 82
Fahr: 200   Celsius: 93
Fahr: 220   Celsius: 104
Fahr: 240   Celsius: 115
Fahr: 260   Celsius: 126
Fahr: 280   Celsius: 137
Fahr: 300   Celsius: 148

Why in the first line he does not the tabulation but in the others yes?

  • 1

    The tabulation is done in columns of four characters each, so in the first as fahr has single character value 0, the word Celsius is moved to the third tabulation column and in the other cases is moved to the fourth column. Format the value with at least two characters of extension with zero fill printf("Fahr: %02d\tCelsius:%d\n", fahr, celsius);

  • 1

    Other option: printf("Fahr: %-2d\tCelsius: %d\n", fahr, celsius). The - indicates to align to the left, and the 2 is the minimum size. Or simply set a larger size and remove the TAB: printf("Fahr: %-10dCelsius: %d\n", fahr, celsius);

1 answer

3

When a \t, he advances to the next tab stop. And this varies according to the size that is configured on the terminal you are using.

For example, in my terminal the size tab stop is 8, so the output went like this:

Fahr: 0 Celsius:-17
Fahr: 20        Celsius:-6
Fahr: 40        Celsius:4
etc...

What happens is that every time there’s a \t, he advances to the next tab stop, which in my case is every 8 characters. It would be something like this (the "rule" below was blatantly copied inspired in this answer):

0       8      16      24
|.......|.......|.......|...
Fahr: 0 Celsius:-17
Fahr: 20        Celsius:-6

In the case, the 0, 8, 16, etc are the tab stops (always multiples of 8, since the size on my terminal is 8).

So in the first case, zero takes position 6, and the \t stands at position 7. Then he advances to the next tab stop, which is position 8.
In the second case, the \t is in position 8, so he advances to the next tab stop, which is heading 16.


In your case (assuming the spaces shown are this way), it seems that the size tab stop it’s 4, so it would look like this:

0   4   8  12  16  20  24
|...|...|...|...|...|...|...
Fahr: 0 Celsius: -17
Fahr: 20    Celsius: -6

Now the tab stops are multiple positions of 4.

In the first case, zero occupies position 6, and the \t stands at position 7. Then he advances to the next tab stop, which is position 8.
And in the second case, the \t is in position 8, so he advances to the next tab stop, which is heading 12.


A solution would be, instead of depending on the tab stop, simply set a fixed size for the numbers. For example, switch to:

printf("Fahr: %-6d Celsius: %d\n", fahr, celsius);

In the case, the - says to line left, and the 6 indicates the size to be used (and filled with spaces, that is, whatever the size of the tab stop, will always use this size). With this the output will be:

Fahr: 0      Celsius: -17
Fahr: 20     Celsius: -6
Fahr: 40     Celsius: 4
Fahr: 60     Celsius: 15
Fahr: 80     Celsius: 26
Fahr: 100    Celsius: 37
Fahr: 120    Celsius: 48
Fahr: 140    Celsius: 60
Fahr: 160    Celsius: 71
Fahr: 180    Celsius: 82
Fahr: 200    Celsius: 93
Fahr: 220    Celsius: 104
Fahr: 240    Celsius: 115
Fahr: 260    Celsius: 126
Fahr: 280    Celsius: 137
Fahr: 300    Celsius: 148

But of course the exact sizes will depend on what you need. For example, if any number has more than 6 digits, it will be misaligned again:

Fahr: 999999 Celsius: 555537
Fahr: 1000019 Celsius: 555548

But then you have to adapt the output for each case. Anyway, the general idea and the explanation are there. You can see in documentation all available options accepted by printf.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.