用C语言遍历0000-9999并装入结构体
时间: 2023-06-21 16:06:09 浏览: 50
可以使用循环语句和结构体来实现这个功能。具体实现如下:
```c
#include <stdio.h>
// 定义结构体
struct number {
int thousands; // 千位数
int hundreds; // 百位数
int tens; // 十位数
int ones; // 个位数
};
int main() {
struct number num[10000]; // 定义结构体数组
// 遍历0000-9999并装入结构体数组
for (int i = 0; i < 10000; i++) {
num[i].thousands = i / 1000;
num[i].hundreds = (i % 1000) / 100;
num[i].tens = (i % 100) / 10;
num[i].ones = i % 10;
}
// 打印结构体数组中的前10个元素
for (int i = 0; i < 10; i++) {
printf("%d%d%d%d\n", num[i].thousands, num[i].hundreds, num[i].tens, num[i].ones);
}
return 0;
}
```
运行结果如下:
```
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
```