文件范围和全局范围:C&放大器; C ++ [英] File Scope and Global Scope: C & C++

查看:188
本文介绍了文件范围和全局范围:C&放大器; C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学生,我感到困惑的全球和C和C文件作用域++
谁能解释有两种观点有什么区别如果是请解释在小细节

I am student and I am confused about global and file scope in C and C++ Can anyone explain is there any difference in both perspective if yes please, Explain in little detail

推荐答案

与文件范围内变量可以通过一个单一的文件中的任何功能或模块进行访问。要声明文件范围的变量,只需声明块(同一个全局变量)的变量之外,但使用static关键字。

A variable with file scope can be accessed by any function or block within a single file. To declare a file scoped variable, simply declare a variable outside of a block (same as a global variable) but use the static keyword.

static int nValue; // file scoped variable
float fValue; // global variable

int main()
{
    double dValue; // local variable
}

文件范围变量的行为完全一样的全局变量,但它们的使用仅限于它们所声明的文件。

File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared.

这篇关于文件范围和全局范围:C&放大器; C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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