1
#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.

decfsl
  • 31
  • 3
  • 1
    What do you mean with _\t doesn't work_? – pzaenger Aug 02 '16 at 12:25
  • 2
    It works, but it doesn't take more than one space in the visual representation? Try to run it trough a hexdump, or copy&paste it to an editor where there is special visualization of whitespace characters. – moooeeeep Aug 02 '16 at 12:26
  • A tab does not insert x spaces. It aligns the output to columns in the console window. – 001 Aug 02 '16 at 12:28
  • here tab is for 4 places. So in case of first `\t` it will show only one space as first there places are occupied by the first three characters i.e. `=10` – The Philomath Aug 02 '16 at 12:28
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Aug 02 '16 at 12:35
  • I gave you an example of a better way to format output – xenteros Aug 02 '16 at 12:38

4 Answers4

7

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.

rustyx
  • 80,671
  • 25
  • 200
  • 267
2

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
xenteros
  • 15,586
  • 12
  • 56
  • 91
1

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);
sps
  • 2,720
  • 2
  • 19
  • 38
0

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.

blue112
  • 52,634
  • 3
  • 45
  • 54