Question about extern keyword

F

fl

Hi,

I have small question about "extern" keyword. I browse website on line. Then, I have a new question about a post relevant to extern on a forum.

http://stackoverflow.com/questions/11756995/how-to-use-extern-in-c

The original post is pasted here:
..................

I have this code:
#include <stdio.h>


extern int x;
void a(){
int x =100;
printf("%d ",x);
x+=5;
}

void b(){
static int x = -10;
printf("%d ",x);
x+=5;
}

void c(){
printf("%d ",x);
x+=2;
}

int main(){
int x=10;
a();
b();
c();
a();
b();
c();
printf("%d ",x);
getchar();
return 0;
}

int x = 0;


I was sure that the fact that extern in declared here, I will have a compilation error - but everything passed. also , what is the meaning of extern when it's inside the C file itself? shouldn't it be in another file? Is there a way to declare this variable in order for this not to complile?
........

My question is about the last paragraph. Although I can understand its answer replies by others, I do not understand the original poster.

The snippet can build, why he said: "I was sure that the fact that extern in declared here, I will have a compilation error - but everything passed." Is there a typo, or something omitted, in this line?

The second question is about the last line: "Is there a way to declare thisvariable in order for this not to complile?" It was possible the original poster could not build the program with his compiler. Then, I still can understand his first comments (about my first question).

Could you help me on the understanding this poster?

Thanks a lot
 
S

Shao Miller

Hi,

I have small question about "extern" keyword. I browse website on line. Then, I have a new question about a post relevant to extern on a forum.

http://stackoverflow.com/questions/11756995/how-to-use-extern-in-c

The original post is pasted here:
.................

I have this code:

The indentation was horrible, so I hope it's a little clearer thanks to
the 'indent' command:
#include <stdio.h>

extern int x;
void
a ()
{
int x = 100;
printf ("%d ", x);
x += 5;
}

void
b ()
{
static int x = -10;
printf ("%d ", x);
x += 5;
}

void
c ()
{
printf ("%d ", x);
x += 2;
}

int
main ()
{
int x = 10;
a ();
b ();
c ();
a ();
b ();
c ();
printf ("%d ", x);
getchar ();
return 0;
}

int x = 0;


I was sure that the fact that extern in declared here, I will have a compilation error - but everything passed. also , what is the meaning of extern when it's inside the C file itself? shouldn't it be in another file? Is there a way to declare this variable in order for this not to complile?
.......

My question is about the last paragraph. Although I can understand its answer replies by others, I do not understand the original poster.

Since the poster is mistaken, it's no wonder that you can't understand.
The snippet can build, why he said: "I was sure that the fact that extern in declared here, I will have a compilation error - but everything passed." Is there a typo, or something omitted, in this line?

Suppose I say "2 + 2 = 5, for large values of 2."

Then you ask, "What did he mean?"

This is a silly question to ask if you understand that I am wrong. If
you really want to know the answer for some reason, why don't you ask
the original poster? If you ask comp.lang.c, you will only get guesses.

Here is a guess: The poster thinks that 'extern' means that 'x' cannot
be defined in the same source file. That is incorrect.
The second question is about the last line: "Is there a way to declare this variable in order for this not to complile?" It was possible the original poster could not build the program with his compiler. Then, I still can understand his first comments (about my first question).

If the last line of the program is removed, then there is no definition
for the file-scope 'x', so the program will no longer compile.
Could you help me on the understanding this poster?

I think it is very simple: They are mistaken in their understanding of
what 'extern' means, in C.
 
B

Bart van Ingen Schenau

My question is about the last paragraph. Although I can understand its
answer replies by others, I do not understand the original poster.

The snippet can build, why he said: "I was sure that the fact that
extern in declared here, I will have a compilation error - but
everything passed." Is there a typo, or something omitted, in this line?

There are a few typo's that might make it harder to understand, and I
think the OP is not a native English speaker.
What the OP expresses is that he expected the use of 'extern' to trigger
a diagnostic from the compiler, but the compiler did not complain.
The second question is about the last line: "Is there a way to declare
this variable in order for this not to complile?" It was possible the
original poster could not build the program with his compiler. Then, I
still can understand his first comments (about my first question).

Could you help me on the understanding this poster?

He was asking how to *break* his build, not how to fix a problem he
encountered.
The reasoning was probably that by studying how the compiler reacts to
broken programs, you get a better understanding of how a compiler works.
Thanks a lot

Bart v Ingen Schenau
 
B

Ben Bacarisse

fl said:
I have small question about "extern" keyword. I browse website on
line. Then, I have a new question about a post relevant to extern on a
forum.

http://stackoverflow.com/questions/11756995/how-to-use-extern-in-c

The original post is pasted here:
.................

I have this code:
#include <stdio.h>


extern int x;
void a(){
int x =100;
printf("%d ",x);
x+=5;
}

void b(){
static int x = -10;
printf("%d ",x);
x+=5;
}

void c(){
printf("%d ",x);
x+=2;
}

int main(){
int x=10;
a();
b();
c();
a();
b();
c();
printf("%d ",x);
getchar();
return 0;
}

int x = 0;


I was sure that the fact that extern in declared here, I will have a
compilation error - but everything passed. also , what is the meaning
of extern when it's inside the C file itself? shouldn't it be in
another file? Is there a way to declare this variable in order for
this not to complile?
.......

My question is about the last paragraph. Although I can understand its
answer replies by others, I do not understand the original poster.

The snippet can build, why he said: "I was sure that the fact that
extern in declared here, I will have a compilation error - but
everything passed." Is there a typo, or something omitted, in this
line?

It seems clear to me. The original poster (OP) thought there was an
error because he or she had only every seen extern used to declare a
variable which is defined in another translation unit. It turns out
that extern is more subtle than the OP thought so there is in fact no
error in this code.
The second question is about the last line: "Is there a way to declare
this variable in order for this not to complile?" It was possible the
original poster could not build the program with his compiler. Then, I
still can understand his first comments (about my first question).

I can't see a question (from you) here so I don't know what you are
asking. For what it's worth, the OP's question about how to make
something "not compile" is silly at all sorts of levels but presumably
that was explained in the original posting on stackoverlow.

<snip>
 
B

BartC

Shao Miller said:
On 1/12/2013 06:24, fl wrote:
Here is a guess: The poster thinks that 'extern' means that 'x' cannot be
defined in the same source file. That is incorrect.

(Having both an extern declaration and definition of a variable in one file,
makes it possible to share the same header, containing the extern
declaration, amongst several files, without having to make a special case
for the file where the variable actually lives.

So that sort of makes sense. But what seem to make less sense, is being able
to have any number of 'extern x;' in a file, as well as any number of 'int
x;', at file scope! Although only one of those latter can be initialised.)

However, having extern x; int x; in more than one file will likely cause a
link error.
 
B

Ben Bacarisse

BartC said:
(Having both an extern declaration and definition of a variable in one file,
makes it possible to share the same header, containing the extern
declaration, amongst several files, without having to make a special case
for the file where the variable actually lives.

So that sort of makes sense. But what seem to make less sense, is being able
to have any number of 'extern x;' in a file, as well as any number of 'int
x;', at file scope! Although only one of those latter can be
initialised.)

It would have been clearer to write "extern int x;". "extern x;" is no
longer meaningful in C99 and later and it is likely to confuse some
readers.
However, having extern x; int x; in more than one file will likely cause a
link error.

It is also *certain* to make the program undefined (as far as standard C
is concerned), so even in the absence of a link error one should avoid
doing it. Also, in C99 and later there must be diagnostic because
"extern x;" is a constraint violation.

(Just having "int x;" at files scope in two translation units is enough
to have undefined behaviour -- the extern declaration makes it no better
and no worse.)
 
J

James Kuyper

On 01/12/2013 08:11 AM, Ben Bacarisse wrote:
....
I can't see a question (from you) here so I don't know what you are
asking. For what it's worth, the OP's question about how to make
something "not compile" is silly at all sorts of levels but presumably
that was explained in the original posting on stackoverlow.

It's not really silly, it's just a misunderstanding. My best guess is
that, according to his misunderstanding of 'extern', a declaration using
that keyword is incompatible with a definition of the same identifier at
file scope in the same translation unit.

He wanted to know what he needed to do to get the compiler to notice
that this "mistake" had been made, so it could warn him to prevent him
from making similar mistakes in the future.

With the correct understanding of what 'extern' means, his code is not a
mistake, so no diagnosis is required.
 
K

Keith Thompson

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top