小编cra*_*ist的帖子

使用 for 循环在 C 中运行时初始化字符数组

在运行时使用 for 循环语句初始化字符数组时,会像 printf 函数一样执行两次,同时 6 大小的字符数组只需要 3 个输入:

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

int main() {

  int i = 0;
  char name[6];

  for (int i = 0; i < 5; ++i)
  {
    printf("Enter the character in  name array");
    scanf("%c", &name[i]);
  }

  printf("%s", name);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

-1
推荐指数
1
解决办法
179
查看次数

标签 统计

c ×1