#include 声明数组int array[size];其中,size 是数组的大小。使用循环遍历数组for (int i = 0; i < size; i++) {">

c语言中怎么显示数组

来源:这里教程网 时间:2026-02-21 16:49:48 作者:

如何显示 C 语言中的数组

在 C 语言中,可以通过以下步骤显示数组中的元素:

    包含头文件
<code class="c">#include <stdio.h></code>
    声明数组
<code class="c">int array[size];</code>

其中,

size
是数组的大小。

    使用循环遍历数组
<code class="c">for (int i = 0; i < size; i++) {
  // 访问和输出数组中的每个元素
  printf("%d ", array[i]);
}</code>

示例代码:

立即学习“C语言免费学习笔记(深入)”;

<code class="c">#include <stdio.h>
int main() {
  int array[] = {1, 2, 3, 4, 5};
  int size = sizeof(array) / sizeof(array[0]);
  for (int i = 0; i < size; i++) {
    printf("%d ", array[i]);
  }
  return 0;
}</code>

输出:

<code>1 2 3 4 5</code>

相关推荐