extern External linkage declarations

extern External linkage declarations

extern declares external names. Uninitialized file-scope objects are not defined; sharing needs one matching definition, while initialized extern defines.

CategoryStorage-class specifiers
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

extern int g_count;  /* Declaration, not a definition */

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

shared value = 42
ℹ️ Info To keep this single-file example runnable, shared_value is first declared with extern and defined exactly once at the end. Multi-file projects usually put the declaration in a header and the definition in one source file.

📚 Related Tutorials

❓ FAQ

QHow do extern int x and int x differ at file scope?
Aextern int x is normally only a declaration, while int x without a storage-class specifier is a tentative definition that can produce a zero-initialized definition. A shared object must ultimately have exactly one definition.
QWhere should a variable declared with extern be defined?
AProvide exactly one definition with a compatible type in a translation unit linked into the program. Other translation units refer to it through extern declarations. It need not be in a different physical file, but the program must satisfy the one-definition rules.
QIs extern int x = 1 a declaration or a definition?
AAt file scope, an extern declaration with an initializer is also a definition and allocates the object. It is not merely a reference to another definition. Avoid this form in headers shared across files because it can cause multiple definitions.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏