Remove white space at the end of the output in C

后端 未结 6 1357
离开以前
离开以前 2021-01-27 06:59

The following code is for printing the elements of a matrix in spiral order. The program works fine. The problem, however, is that the online compiler against which I\'m checkin

6条回答
  •  日久生厌
    2021-01-27 08:06

    You need to suppress the space when you don't need one. You can do it like this:

    Add these declarations:

    char *format = "%d";
    int first_number = 1;
    

    Add this after the first printf:

    if (first_number) {
        /* Now we want a space between numbers */
        first_number = 0;
        format = " %d";
    }
    

    Change your printf:s to use the new variable:

                printf(format, ...);
    

提交回复
热议问题