简体   繁体   中英

C program output not formatting properly(using \t)

I am just wondering and confused as too why my output is messed up. The code is below. I am a beginner so please excuse my lack of skill. Any help would be very much appreciated.

#include <stdio.h>

int main(int argc, char *argv[]) 
{

    for (int i = 1; i <= 10; i++)
    {

        for (int j = 1; j <= i; j++)
        {
            printf("*");
        }
        printf("\t");
        for (int k = 1; k <= 11-i; k++)     
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Gives this output:

在此输入图像描述

My desired output is this:
在此输入图像描述

\\t stops at the next reachable tab position , which by default are multiples of 8.

Therefore, if you print *** , it will skip to column 8, but if you print ********* , you are already past 8, so it skips to column 16.

The precision and width fields can be use for formatting. Precision will print up to the specified number of characters and width will print at least the specified number of characters. Using an asterisk in either field allows an argument for a variable number of characters.

#include <stdio.h>

int main( void) {
    char asterisk[] = "***********";

    for (int i = 1; i <= 10; i++)
    {
        printf ( "%.*s", i, asterisk);//print up to i characters
        printf ( "%*s", 15 - i, " ");//print at least 15 - i characters
        printf ( "%.*s", 11 - i, asterisk);
        printf("\n");
    }
    return 0;
}

... why my output is messed up (?)

Code uses a tab '\\t' which only aligns to the next tab-stop - usual every 8 columns and the stars needed exceed 8.


With judicious use of the '-' flag , field width and precision , code can be simplified to print an array of characters.

#include <stdio.h>
#include <string.h>

int main(void) {
  int n = 10;
  char stars[n];
  memset(stars, '*', n);

  for (int i = 0; i < n; i++) {
    //       +------------- - flag: pad on right     
    //       |+------------ field width: min characters to print, pad with spaces
    //       || +--------+- precision: max numbers of characters of the array to print
    printf("%-*.*s     %.*s\n", n, i + 1, stars, n - i, stars);
    //                          |  ^^^^^         ^^^^^ precision
    //                          +--------------------- field width
  }
}

Output

*              **********
**             *********
***            ********
****           *******
*****          ******
******         *****
*******        ****
********       ***
*********      **
**********     *

Note: stars is not a string as it lacks a null character . Use of a precision with "%s" allows printf() to use a simple character array. Characters from the array are written up to (but not including) a terminating null character.

A tab-stop is not a fixed-width space (eg the same as 4 spaces or 8 spaces), it means that the output device should move the caret (or print-head) to the next column position for tabular data. These column positions are at fixed regular intervals, that's why \\t** and **\\t have different printed widths:

String        Output:
"\t**a"       "    **a" (7 wide)
"**\ta"       "**  a"   (5 wide)

As others have mentioned, printing a tab character move the cursor to the next tab stop, not a certain number of spaces.

After printing the first set of asterisks, print spaces until you've printed enough characters to space out as far as you need.

    for (int j = 1; j <= 10; j++)
    {
        if (j<i) {
            printf("*");
        } else {
            printf(" ");
        }
    }

I would do like this:

const int rows = 10;
for (int i = 0; i < rows; i++) {
    const int width = 10;
    const int space = 5;
    const int totalw = 2*width + space;
    for (int j = 0; j < totalw; j++) {
        char ch = j<=i || (j>=width+space && j<(totalw-i)) ? '*' : ' ';
        printf("%c", ch);
    }
    printf("\n");
}

And just for completeness, even though mentioned in other answers. The '\\t' character does not have a fixed width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM