Question about extern keyword

Discussion in 'C Programming' started by fl, Jan 12, 2013.

  1. fl

    fl Guest

    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
    fl, Jan 12, 2013
    #1
    1. Advertising

  2. fl

    Shao Miller Guest

    On 1/12/2013 06:24, fl wrote:
    > 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.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Jan 12, 2013
    #2
    1. Advertising

  3. On Sat, 12 Jan 2013 03:24:20 -0800, fl wrote:
    >
    > 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
    Bart van Ingen Schenau, Jan 12, 2013
    #3
  4. fl <> writes:

    > 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>
    --
    Ben.
    Ben Bacarisse, Jan 12, 2013
    #4
  5. fl

    BartC Guest

    "Shao Miller" <> wrote in message
    news:kcrku9$40a$...
    > On 1/12/2013 06:24, fl wrote:


    >> 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.


    > 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.

    --
    bartc
    BartC, Jan 12, 2013
    #5
  6. "BartC" <> writes:

    > "Shao Miller" <> wrote in message
    > news:kcrku9$40a$...
    >> On 1/12/2013 06:24, fl wrote:

    >
    >>> 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.

    >
    >> 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.)


    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.)

    --
    Ben.
    Ben Bacarisse, Jan 12, 2013
    #6
  7. fl

    James Kuyper Guest

    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.
    --
    James Kuyper
    James Kuyper, Jan 12, 2013
    #7
  8. Shao Miller <> writes:
    > On 1/12/2013 06:24, fl wrote:
    >> 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

    [snip]
    >>
    >> 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:

    [...]

    I've fixed the indentation on stackoverflow.com.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 12, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,404
    tom_usenet
    Aug 2, 2004
  2. tweak

    keyword extern

    tweak, Jun 26, 2004, in forum: C Programming
    Replies:
    18
    Views:
    645
    Dan Pop
    Jun 29, 2004
  3. ooze

    how to use the keyword extern in c?

    ooze, Aug 25, 2004, in forum: C Programming
    Replies:
    16
    Views:
    638
    Dan Pop
    Aug 30, 2004
  4. siliconwafer

    Use of 'extern' keyword

    siliconwafer, Jul 28, 2005, in forum: C Programming
    Replies:
    5
    Views:
    379
    Kenny McCormack
    Jul 31, 2005
  5. Andre
    Replies:
    5
    Views:
    532
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page