Reputation: 91
Consider this example:
#include <stdio.h>
int main() {
int fahr, celsius;
int lower, upper, step;
lower = 0; upper = 300; step = 20;
fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr += step;
}
return 0;
}
And get output like this:
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
since '\t' represent four white spaces, why the output so neat? It seems that the following output format is in accordance with the first one. Thanks for help!
Upvotes: 0
Views: 2634
Reputation: 419
/t
is itself a character. You cannot mix it up with spaces. It is just like other characters. It too has an ASCII code (09) just like others.
Upvotes: -1
Reputation: 44368
since '\t' represent four white spaces ...
Well, that's wrong. Tab (\t
) is not four spaces (or any other fixed number of spaces).
Instead it means: Move to the next tab-stop.
The distance between tab-stops are a fixed number. So printing \t
will move the cursor to a position that can be written as N * Tab-size
.
But notice that tab-size may differ from terminal to terminal. Typically it's either 4 or 8 but it can be other values as well.
Example with tab-size equal 4:
Current position Next tab-stop (i.e. new position after printing \t)
0 4
1 4
2 4
3 4
4 8
5 8
6 8
7 8
8 12
and so on.
In your case as long as printing fahr
requires 3 (or less) characters, celsius
will always be printed at position 4.
However, if printing fahr
requires 4 characters, you'll see celsius
being printed at position 8.
Changing upper
to 1200 and step
to 200 will give:
0 -17
200 93
400 204
600 315
800 426
1000 537
1200 648
At the first 5 lines celsius
is printed at position 4 because printing fahr
requires less than 4 characters and consequently the next tab-stop is at position 4.
At the two last lines celsius
is printed at position 8 because printing fahr
requires 4 characters and consequently the next tab-stop is at position 8.
Upvotes: 3