How to Print Array in C with printf
In C language, you can iterate through each element of an array using a loop and use the printf function to sequentially output the elements of the array. Here is an example code:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]); // 计算数组长度
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]); // 使用printf函数输出数组元素
}
return 0;
}
Output:
1 2 3 4 5
In the example above, we defined an integer array called arr and initialized some values. Within a for loop, we used the printf function to output each element of the array.