为什么我不能在 C 中的函数之外为全局变量赋值? [英] Why can't I assign values to global variables outside a function in C?

查看:32
本文介绍了为什么我不能在 C 中的函数之外为全局变量赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个全局变量,我想为它分配另一个变量.我发现您可以为函数内的全局变量分配另一个值:

Suppose I have a global variable, and I want to assign another variable to it. I've found out that you can assign another value to a global variable inside a function:

int i = 8;

int main(void)
{
  i = 9;     /* Modifies i */
  return 0;
}

但是,在函数外分配全局变量是行不通的!

However, assignment of the global variable outside of a function does not work!

int i = 8;

i = 9;  /* Compiler error */

int main(void)
{
  return 0;
}

我收到以下错误消息:

warning: data definition has no type or storage class
warning: type defaults to 'int' in declaration of 'i'
  error: redefinition of 'i'
note: previous definition of 'i' was here
int i = 8;
    ^

为什么会这样?

推荐答案

这是一个全局变量的定义,可选择初始化为特定值:

This is a definition of a global variable, with the optional initialisation to a specific value:

int i = 8;

请注意,它不是被执行的代码,变量将被设置为最初包含 8.要么认为它是神奇的"(对于许多标准并未真正定义的事物的有用模型),要么考虑在执行任何代码之前将值复制到内存位置的表.

Note that it is not code which gets ever executed, the variable will just be set up to initially contain the 8. Either consider it "magic" (a helpful model for many things not really defined by the standard) or think of tables with values being copied to memory locations before any code is executed.

这是一段没有执行框架"的代码.
(或者你打算这样做.编译器有其他意见,见下文.)

This is a piece of code which has no "frame" in which it is executed.
(Or you intend it to be. The compiler is of other opinion, see below.)

i = 9;

没有包含它的函数.目前还不清楚应该在什么时候执行.那是编译器不喜欢的.
在 C 中,所有代码都必须在函数内部,并且只有在调用该函数时才会执行,例如来自 main().

There is no function containing it. It is not clear when it should be executed. That is what the compiler does not like.
In C, all code has to be inside a function and will only be executed if that function is called, e.g. from main().

其他语言,主要是那些通过解释脚本"来执行脚本"的语言(而不是将代码转换为可执行文件,例如由编译器)允许在任何地方拥有代码.C不一样.

Other language, mostly those which execute "scripts" by interpreting them (instead of code being turned into executeables, e.g. by a compiler) allow to have code anywhere. C is different.

编译器对此有不同的看法:

The compiler sees this differently:

i = 9;

  • 它不在函数内部,所以不能是代码
  • 它看起来像一个变量定义,假设你的意思是它是一个 int,即默认值
  • 但是依赖默认值不是一个好主意,所以警告缺少类型并使用默认值
  • 另外,如果它一个定义,那么它是i的第二个,现在这真的是错误的,所以显示一个错误并且编译失败
  • 为了有帮助,请提及 i 的第一个定义在哪里
    • it is not inside a function, so it cannot be code
    • it looks like a variable definition, assuming that you mean it to be an int, i.e. the default
    • but relying on defaults is not a good idea, so warn about missing type and that the default is used
    • also, if it is a definition, then it is the second one for i, now that is really wrong, so show an error and fail the compiling
    • just to be helpful, mention where the first definition of i is
    • 这就是如何读取您引用的编译器输出.

      That is how to read the compiler output you have quoted.

      这篇关于为什么我不能在 C 中的函数之外为全局变量赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆