lufork lufork - 2 months ago 4
C Question

Why isn't this C code's \t working?

I am trying to print a pattern of numbers. Try running this code with a input of 20, you will see that the tab spaces are all in the wrong place, they don't follow the order. I know that the tab spaces jump to the next header, but is there a way to avoid it?

#include <stdio.h>

int main()
{
int i, n, count = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("%dnumber\t", i);
count++;
if(count == 4)
{
count = 0;
printf("\n");
}
}
return 0;
}


Note: Is there a way to do this with tab spaces only(i.e., "\t" only) instead of using an ordinary white space.

Here is the output that I am getting.

enter image description here

But what I want is this

enter image description here

Answer

Why not:

#include <stdio.h>

int main( void )
{
    int i = 0;
    int n = 0;

    scanf( "%d", &n );

    for( i = 1; i <= n; i++ )
    {
        printf( "%02dnumber\t", i );

        if( i % 4 == 0 )
            printf("\n");
    }

    return 0;
}

Output:

16
01number    02number    03number    04number    
05number    06number    07number    08number    
09number    10number    11number    12number    
13number    14number    15number    16number    

Hope it helps!

Comments