【问题标题】:Output array as a "String" separated by commas in c将数组输出为“字符串”,在 c 中用逗号分隔
【发布时间】:2018-10-15 05:10:01
【问题描述】:

我有以下数组:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main () {

int openedLockers[5] = {0,1,2,3,4};


return 0;
}

并且想输出“Opened Lockers: 0,1,2,3,4。”

它必须以最后一个数字后的句点结束。我不确定如何在 for 循环中执行此操作,而不必多次打印“Opened Lockers:”部分。

【问题讨论】:

  • 在进入循环之前打印"Opened Lockers: " 部分......
  • 为了将来参考,我(强烈)怀疑您收到的反对票是由于缺少显示您尝试过的内容以及卡在哪里的努力。我不是为此而寻找新用户的人,而是试图引导他们尽快阅读About 页面,并访问描述How to Ask a QuestionHow to create a Minimal, Complete, and Verifiable example 的链接。如果遵循,并且如果您先在此处搜索重复项,则将消除反对票。

标签: c arrays output


【解决方案1】:
#include <stdio.h>

int main () {

int openedLockers[] = {0,1,2,3,4};

printf("Opened Lockers: ");

for (size_t i = 0; i < 5; i++) {
    printf("%d", openedLockers[i]);

    if (i != 4)
        printf(",");
    else
        printf(".");
}
return 0;
}


// Output : Opened Lockers: 0,1,2,3,4.

【讨论】:

  • 值得一试,但如果您添加的值多于声明的元素,您应该将其设为int openedLockers[] = ... 以避免潜在的错误。不需要stdlib.hstdbool.h
  • 在我的情况下,数组的最后一个数字是未知的,但它对此代码进行了简单的更改,谢谢。
  • 在 C 语言中至少有十几种方法可以给任何一只猫剥皮 :)
  • i 应该是 size_t 类型。
【解决方案2】:

实现所需格式的一种超级简单的方法是在输出循环中使用条件(三元)运算符作为printf 格式字符串的一部分,例如

#include <stdio.h>

int main (void) {

    int openedLockers[] = {0,1,2,3,4},
        n = sizeof openedLockers / sizeof *openedLockers;

    fputs ("Opened Lockers: ", stdout);

    for (int i = 0; i < n; i++)
        printf (i ? ",%d" : "%d", openedLockers[i]);
    puts (".");

    return 0;
}

使用/输出示例

$ ./bin/openlockers
Opened Lockers: 0,1,2,3,4.

查看一下,如果您还有其他问题,请告诉我。

【讨论】:

  • in 的类型应该是 size_tn 最有可能是 const-qualified。
  • 是的,我同意,但与其将其添加到讨论中并且值限制为 5,我们将其全部保留在 int 此处 :) 虽然您可以 const 限定n,要让const 符合i 是相当困难的。
【解决方案3】:

要在没有分支的情况下做到这一点:

#include <stddef.h>
#include <stdio.h>

int main (void)
{
    int openedLockers[] = {0, 1, 2, 3, 4};
    size_t const n = sizeof openedLockers / sizeof *openedLockers;

    printf("Opened Lockers: ");

    for (size_t i = 0; i < n; ++i)
        printf("%d,", openedLockers[i]);

    puts("\b."); // overwrite the last comma with a period and add a newline.
}

【讨论】:

    【解决方案4】:
    #include <stddef.h>
    #include <stdio.h>
    
    int main (void)
    {
        int openedLockers[] = {0, 1, 2, 3, 4};
        int const n = sizeof openedLockers / sizeof *openedLockers;
    
        printf("Opened Lockers: ");
    
        for (int i = 0; i < n; ++i)
            printf("%d,", openedLockers[i]);
    
        printf("\b.");
    }
    

    【讨论】:

    • 您包含&lt;stddef.h&gt; 的目的是什么?
    【解决方案5】:

    仅循环重复部分以避免ifs 或其他条件构造。请注意,重复部分的重复次数比元素的数量少一倍。

    // before repeats, include first element
    printf("Opened Lockers: %d", openedLockers[0]);
    
    // repeat comma, space, element
    for (int i = 1; i < 5; i++) {
        printf(", %d", openedLockers[i]);
    }
    
    // after repeats
    printf(".\n");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多