Clarification required for linkage.

S

somenath

Hi All,

I would like to get my understanding verified for the following.

#include<stdio.h>
int g;
int g =23;
int main(void)
{
printf("\n g= %d\n",g);
return 0;
}

The question is why compilation of the code is error free.
According to me
int g;is the tentative definition of g and it has linkage as if the
"extern" keyword had been present. So it has external linkage.

In case of int g =23;
It is non tentative definition of g and the has linkage as if the
"extern" keyword had been present. So it has external linkage.

According to the C standard If, within a translation unit, the same
identifier appears with both internal and external linkage the
behavior is not defined.

As g in both the case has external linkage that’s the reason compiler
is not throwing error.

Please correct me if I am wrong.


Regards,
Somenath
 
P

Peter Nilsson

somenath said:
I would like to get my understanding verified for the
following.

#include<stdio.h>
int g;
int g =23;
int main(void)
{
  printf("\n g= %d\n",g);
  return 0;
}

The question is why compilation of the code is error free.
As g in both the [declarations] has external linkage that’s
the reason compiler is not throwing error.

Correct. But note that such code is unusual.
 
S

somenath

Correct. It's a tentative definition with external linkage. But there is a
subtle point. Note carefully the wording of 6.2.2{5}:

  If the declaration of an identifier for a function has no storage-
  class specifier, its linkage is determined exactly as if it were
  declared with the storage-class specifier extern. If the declaration
  of an identifier for an object has file scope and no storage-class
  specifier, its linkage is external.  

See how there's no "as if it were declared with ... extern" for object
declarations as there is for function declarations? To understand why,
suppose your code was missing the "int g = 23;" and had only the tentative
definition "int g;". If the object had an implied "extern", i.e., "extern
int g;", then you wouldn't end up with a definition by the end of
translation as you would with "int g;". The Standard would allow "extern
int g;", even though there's no corresponding definition in the program as
a whole.

So this means my assumption there would be hidden extern presents not
true though int g; has external linkage
But in case of function it is as if there is extern present. i.e void
fun(); is treated by compiler as extern void fun();
Kindly let me know if it is correct?

So does extern only specify external linkage when a variable is
declared inside function?
For example

void func()
{
extern int g;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top