gpt4 book ai didi

c - 使用数组生成序列?

转载 作者:太空宇宙 更新时间:2023-11-04 03:49:39 24 4
gpt4 key购买 nike

我试图获取从 2 到 N 的整数列表,但是我得到了一个奇怪的输出。

代码:

#define N 10

int main(int argc, char *argv[]) {

int array[N]={2-N};
for(int i=0;i<N;printf("%d ",array[i]), i++);
}

输出:

-8 0 0 0 0 0 0 0 0 0 

我试着让它读成:2,3,4,5,6,7,8,9,10

有什么想法吗?提前致谢

最佳答案

这个声明

int array[N]={2-N};

将数组的第一个元素设置为 2-N,由于 N8,因此会将其设置为 -8。其他元素未定义,但恰好为零。

你想要类似的东西

for (int i = 0; i < N; i++)
array[i] = i+2;

关于c - 使用数组生成序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com