gpt4 book ai didi

c - 我们不能在全局范围内编写任何赋值语句,为什么?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:51 25 4
gpt4 key购买 nike

为什么我们不能在全局写任何赋值语句?我的代码如下

  #include <stdio.h>
static int i=10; //Initialization statement
i=25; //Assignment statement not possible why?
int main()
{
printf("%d",i);
return 0;
}

最佳答案

这是一个“何时”的问题。

int i=10; 是在编译时确定的,i 在程序加载或首次加载时采用 10 的值开始运行。

全局 i=25;无序列。对于多个链接文件,无法控制这些“全局”代码的运行顺序 - 除了可能的链接顺序。

通过将 int i=10; 移动到 main() 中,确定求值顺序。

#include <stdio.h>
static int i=10;

int main(void) {
printf("%d",i);
i=25;
printf("%d",i);
return 0;
}

关于c - 我们不能在全局范围内编写任何赋值语句,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28379290/

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