如何在函数内声明全局变量?

时间:2022-06-29 00:40:57

I have problem creating global variable inside function, this is simple example:

我在函数内部创建全局变量时遇到问题,这是一个简单的例子:

int main{
   int global_variable;  //how to make that
}

This is exactly what I want to do:

这正是我想要做的:

int global_variable;
int main{
                   // but I wish to initialize global variable in main function
}

4 个解决方案

#1


16  

You have two problems:

你有两个问题:

  1. main is not a loop. It's a function.

    主要不是循环。这是一个功能。

  2. Your function syntax is wrong. You need to have parentheses after the function name. Either of these are valid syntaxes for main:

    你的函数语法错了。您需要在函数名后面加括号。这些都是main的有效语法:

    int main() {
    }
    
    int main(int argv, const char* argv[]) {
    }
    

Then, you can declare a local variable inside main like so:

然后,您可以在main中声明一个局部变量,如下所示:

int main() {
  int local_variable = 0;
}

or assign to a global variable like so:

或者像这样分配一个全局变量:

int global_variable;

int main() {
  global_variable = 0;
}

#2


7  

There is no way to declare it the way you want. And that's it.

没有办法按照你想要的方式声明它。就是这样。

But:

但:

  • First, if you want you can declare it before the main body but assign a value to it inside main. Look Paul's answer for that
  • 首先,如果你想要你可以在主体之前声明它,但在main中为它分配一个值。看看保罗对此的回答
  • Second, actually there is no advantage of declaring variables the way you want. They are global and that means they should be declared in the global scope and no other places.
  • 其次,实际上没有以你想要的方式声明变量的优势。它们是全球性的,这意味着它们应该在全球范围内宣布,而不是其他地方。

#3


6  

int global_variable;
int main()
{
               global_variable=3; // look you assigned your value.
}

#4


0  

well... its indirectly possible by declaring pointers global, and later assigning local variables to them, but sometimes it may lead to situations where pointed variable is unaccessible .

好吧...通过将指针声明为全局,然后将局部变量分配给它们可以间接实现,但有时可能会导致指向变量无法访问的情况。

#1


16  

You have two problems:

你有两个问题:

  1. main is not a loop. It's a function.

    主要不是循环。这是一个功能。

  2. Your function syntax is wrong. You need to have parentheses after the function name. Either of these are valid syntaxes for main:

    你的函数语法错了。您需要在函数名后面加括号。这些都是main的有效语法:

    int main() {
    }
    
    int main(int argv, const char* argv[]) {
    }
    

Then, you can declare a local variable inside main like so:

然后,您可以在main中声明一个局部变量,如下所示:

int main() {
  int local_variable = 0;
}

or assign to a global variable like so:

或者像这样分配一个全局变量:

int global_variable;

int main() {
  global_variable = 0;
}

#2


7  

There is no way to declare it the way you want. And that's it.

没有办法按照你想要的方式声明它。就是这样。

But:

但:

  • First, if you want you can declare it before the main body but assign a value to it inside main. Look Paul's answer for that
  • 首先,如果你想要你可以在主体之前声明它,但在main中为它分配一个值。看看保罗对此的回答
  • Second, actually there is no advantage of declaring variables the way you want. They are global and that means they should be declared in the global scope and no other places.
  • 其次,实际上没有以你想要的方式声明变量的优势。它们是全球性的,这意味着它们应该在全球范围内宣布,而不是其他地方。

#3


6  

int global_variable;
int main()
{
               global_variable=3; // look you assigned your value.
}

#4


0  

well... its indirectly possible by declaring pointers global, and later assigning local variables to them, but sometimes it may lead to situations where pointed variable is unaccessible .

好吧...通过将指针声明为全局,然后将局部变量分配给它们可以间接实现,但有时可能会导致指向变量无法访问的情况。