#include "stdio.h"
int main()
{
int toes = 10;
printf("toes=%d\t2toes=%d\ttoes2=%d\n",toes,toes+toes,toes*toes);
return 0;
}
//This is an exercise from c primer plus. After compiling and running, only the second \t works.
#include "stdio.h"
int main()
{
int toes = 10;
printf("toes=%d\t2toes=%d\ttoes2=%d\n",toes,toes+toes,toes*toes);
return 0;
}
//This is an exercise from c primer plus. After compiling and running, only the second \t works.
It works fine for me. (example)
Output:
toes=10 2toes=20 toes2=100
Note that \t
means "tabulate", or in other words to pad to the nearest multiple of N position, where N is usually 8.
toes=10
takes up 7 characters, so to reach the next multiple of 8 would require printing 1 space.
Tabs indent text to fixed positions. This means, that they don't make 4-space gap but make a gap until matching next flagged position.
toes=10 2toes=20 toes2=100
1234567812345678123456781234567812345678
It's an unlucky example. Your tabs are 8 long. So they stop every 8 letters. If you added one space before first \t
your tab would make a bigger gap.
Better output formatting would be achieved by:
printf("%s%s%s\n", " single", " double", " square");
printf("%7d%7d%7d\n", toes, 2*toes, toes*toes);
Output:
single double square
10 20 100
It is working, but it seems like it is not !!!
This is because by default you have a tabspace
equal to 8
spaces
. And toes=10
takes up 7
spaces, and only 1
space if left for the \t
to fill.
To see that it is working change your printf
to:
printf("ts=%d\t2toes=%d\ttoes2=%d\n",toes,toes+toes,toes*toes);
A \t
character won't take always the same space, it will adjust to a column.
If you want a fix number of spaces between your entries, input them manually between your values.