"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
12 2223
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.
"for is active in the scope surrounding the block"
what does this part mean?? i had a doubt on this one.
What section is that? I am having trouble finding your K&R quote.
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??
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.
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.
donbock
ya you are right the word identifier is missing in my text.
now pls someone respond my query
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.
Can u give an example so that it becomes more clear??
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.
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. - extern int foo1;
-
static int foo2;
-
...
-
{
-
int foo3;
-
...
-
{
-
extern int foo1; // same foo1 as from outer scope
-
extern int foo2; // same foo2 as from outer scope
-
extern int foo3; // same foo3 as from outer scope
-
...
-
}
-
...
-
}
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.
Banfa 9,065
Expert Mod 8TB -
#include <iostream>
-
-
static int var2 = 10;
-
-
int main()
-
{
-
extern int var1;
-
-
if (true)
-
{
-
extern int var2;
-
-
var1 += 20;
-
var2 += 10;
-
}
-
-
std::cout << var1 << " " << var2 << std::endl;
-
}
-
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 -
int var1 = 40;
-
-
int var2 = 60;
-
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.
Thanx Banfa and donbock
You people gave a very good explanation.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
//...
|
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
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...
|
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:
//...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
| |