根据结束条件在 C 中格式化输出

Formatted output in C based on end condition

我正在迭代 C 中的一些计数器以在一行中打印结果,这样分隔 012, 013, 014, etc...

我的代码在某些情况下结束,并且正在被机器人更正。

如何在我的代码的最后一个值之后停止打印 , 。 想法是获得一个只有唯一数字且按升序排列的 3 位数字 以 789;

结尾

我的函数如下:

void my_print_comb(void){
   for(int i = 47; i++ < 57 ; ){
      for(int j = 48; j++ < 57 ; ){
         for(int k = 49; k++ < 57 ; ){
             if( i != j && i != k && j != k){
                while( i < j && j < k){
                char a = i, b = j, c = k;
                my_putchar(a);
                my_putchar(b);
                my_putchar(c);
                my_putchar(',');
                my_putchar(' ');
                break;
                }
             }
         }
      }
   }
}

打印其中一个不带逗号的元素。打印第一个然后遍历数组的其余部分并打印每个元素前面有一个 , ;或打印除最后一个元素之外的所有元素,每个元素后带有 , ,然后打印最后一个元素。我比较喜欢第一种方式

由于您没有显示任何代码,我只是举个例子。

void pretty_print_ints(const int *array, size_t count)
{
    // Nothing to do.
    if (count == 0 || array == NULL) return;

    // Print the first element, without a `,` after it.
    printf("%d", array[0]);

    // If there are more elements in the list, print them all, but add a `, ` separator.
    for (size_t i = 1; i < count; i++) printf(", %d", array[i]);

    // Add a new line at the end.
    printf("\n");
}

如果我们不知道要打印多少元素,它也可以工作。例如,我们可以在遇到等于 0 的元素时停止,而不是在我们打印 count 个元素时停止,我们唯一需要更改的是停止条件。如果我们反其道而行之(使用 printf("%d, ", array[i]); 打印 count - 1 个元素,我们将无法处理这种情况)。