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

查看:399
本文介绍了为什么我不能为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天全站免登陆