• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wjshan0808

Learn from yesterday, Live for today, For a better tomorrow.
 ————wjshan0808

博客园    首页    新随笔    联系   管理    订阅  订阅

'extern'

The external variables in methods are defined by the first lines of the example above, which
state their type and cause storage to be allocated for them. Syntactically, external definitions are just like
definitions of local variables, but since they occur outside of functions, the variables are external. Before a function
can use an external variable, the name of the variable must be made known to the function; the declaration is the
same as before except for the added keyword extern.


In certain circumstances, the extern declaration can be omitted. If the definition of the external variable occurs
in the source file before its use in a particular function, then there is no need for an extern declaration in the
function. In fact, common practice is
to place definitions of all external variables at the beginning of the source file, and then omit all extern
declarations.


If the program is in several source files, and a variable is defined in file1 and used in file2 and file3, then extern
declarations are needed in file2 and file3 to connect the occurrences of the variable. The usual practice is to collect
extern declarations of variables and functions in a separate file, historically called a header, that is included by
#include at the front of each source file. The suffix .h is conventional for header names. The functions of the
standard library, for example, are declared in headers like <stdio.h>.


posted @ 2013-09-26 17:49  wjshan0808  阅读(235)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3