如何声明一个全局变量是present只有一个功能? [英] How to declare a global variable which is present in only one function?

查看:88
本文介绍了如何声明一个全局变量是present只有一个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要声明一个全局变量这是唯一可用如果某个函数被调用。
如果该函数不大于称为该变量不应该是可用的。

I need to declare a global variable which is only available if a certain function is called. If that function is not called than that variable should not be available.

 void function()
 {
   // if this function is called then declare int a=10; 
   // as global so other function can use this 
 }

我如何可以在C做这样的事情?

How can I do such a thing in c?

推荐答案

C不是一个动态语言 - 所有声明的变量存在(受作用域规则),在任何时候都

C is not a dynamic language - all declared variables exist (subject to scoping rules) at all times.

您无法测试一个变量是否已经的声明的,这是编译器的工作,如果你尝试使用一个不在范围内它会给你一个错误。

You cannot test whether a variable has been declared, that's the compilers job, and it'll give you an error if you try to use one that's not in scope.

全球的变量有空间为他们分配(在数据段)时自动在第一次加载程序。

Global variables have space allocated for them (in the "data" segment) automatically when the program is first loaded.

因此​​,你只能测试变量是否已经从原来的赋值改变。

Hence you can only test whether the variable has changed from its original assigned value.

这篇关于如何声明一个全局变量是present只有一个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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