473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

externl linkage

"If a declaration for an identifier within a block does not include the extern specifier, then the identifier has no linkage and is unique to the function. If it does include extern, and an external declaration for is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the same object or function; but if no external declaration is visible, its linkage is external."

this is a statement from K&R.
Can anyone explain me this statement??kindly give an example.
Thanx
Dec 9 '09 #1
12 2223
weaknessforcats
9,208 Expert Mod 8TB
What this means is that any variable between braces {int var; } is local to that pair of braces. That is what is mean by no linkage.

However, if the variable has extern like { extern int var; } then the variable (var) is not between the braces but is outside the braces. In this case there has to be a sharable variable (called external linkage) named var.

But if you omit the the extern keyword, then the variablwe reverts to the first case. That is, a local variable.

Linkage deals with whether something is shared or not between scopes { }.

No linkage means not sharable outside the current { }.
Static linkage means internal linkage which means sharable only in the source file that defines the variable.
Sharable linkage means external linkage which means sharable in another source file.
Dec 9 '09 #2
"for is active in the scope surrounding the block"
what does this part mean?? i had a doubt on this one.
Dec 9 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
What section is that? I am having trouble finding your K&R quote.
Dec 10 '09 #4
well the quote is from my first post.posting it again.

"If it does include extern, and an external declaration for is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the same object or function; but if no external declaration is visible, its linkage is external."

look at the first line.what does "active in the scope " means??
Dec 10 '09 #5
Banfa
9,065 Expert Mod 8TB
You mis-understood, in your first post you said the quote was from K&R, weaknessforcats is asking what the section number in K&R is that contains that quote.
Dec 10 '09 #6
donbock
2,426 Expert 2GB
Kernighan and Ritchie: The C Programming Language, second edition, 1988, ISBN 0-13-110362-8.

Section A11.2 Linkage, second paragraph (page 228)
"If a declaration for an identifier within a block does not include the extern specifier, then the identifier has no linkage and is unique to the function. If it does include extern, and an external declaration for the identifier is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the same object or function; but if no external declaration is visible, its linkage is external."

Perhaps the words "the identifier" are missing from your edition.
Dec 10 '09 #7
donbock

ya you are right the word identifier is missing in my text.
now pls someone respond my query
Dec 11 '09 #8
Banfa
9,065 Expert Mod 8TB
and an external declaration for the identifier is active in the scope surrounding the block
Means that if the variable has already been declared externally and visibly to the block in which it has been declared then the new declaration doesn't result in a new variable but rather it uses the variable that has already been declared rather than defining a new variable which is what happens if an external declaration is not visible outside the block.
Dec 11 '09 #9
Can u give an example so that it becomes more clear??
Dec 11 '09 #10
donbock
2,426 Expert 2GB
You normally use extern to declare a variable that you would otherwise not have access to. That is, the extern statement is the first mention of that variable.
Expand|Select|Wrap|Line Numbers
  1. extern int foo;
  2. foo = 2;
But what happens if the variable already exists in an outer scope? The Standard says that the extern declaration simply refers to that outer-scope variable, and that the linkage of this outer-scope variable is not changed by the extern statement.
Expand|Select|Wrap|Line Numbers
  1. extern int foo1;
  2. static int foo2;
  3. ...
  4.    {
  5.    int foo3;
  6.    ...
  7.       {
  8.       extern int foo1;   // same foo1 as from outer scope
  9.       extern int foo2;   // same foo2 as from outer scope
  10.       extern int foo3;   // same foo3 as from outer scope
  11.       ...
  12.       }
  13.    ...
  14.    }
Notice that the outer-scope variables were already accessible from the inner scope. The extern commands are unnecessary. I would recommend that you not write code like this. In this case, the Standard specifies necessary compiler behavior, not recommended coding style. I suppose there would be a compiler error if the extern statement declared a different type from the outer-scope definition.
Dec 11 '09 #11
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. static int var2 = 10;
  4.  
  5. int main()
  6. {
  7.     extern int var1;
  8.  
  9.     if (true)
  10.     {
  11.         extern int var2;
  12.  
  13.     var1 += 20;
  14.     var2 += 10;
  15.     }
  16.  
  17.     std::cout << var1 << " " << var2 << std::endl;
  18.  
var1 is declared external and there is no declaration visible outside the block so it has external linkage.

var2 is declared external inside the if, however there is a declaration active outside the block which declares var2 static so the var2 inside the if refers to that same object and has the same linkage, that is internal (static) linkage.

This wont link (it will compile) because var1 is declared external, that implies there must be another file where var1 is defined. If you add the file
Expand|Select|Wrap|Line Numbers
  1. int var1 = 40;
  2.  
  3. int var2 = 60;
  4.  
to the project the code now compiles. The externally declared var1 in the first file links against the var1 defined in the second file. The var2 defined in the second file has no effect because the var2 in the first file is declared with internal (static) linkage.

If the first file did not declare var2 static at the top of the file it would have external linkage and you would get a multiply defined symbol error because var2 would be declared with external linkage in both files.
Dec 11 '09 #12
Thanx Banfa and donbock
You people gave a very good explanation.
Dec 11 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.