what is file scope?

Discussion in 'C++' started by Vipul Jain, Sep 25, 2004.

  1. Vipul Jain

    Vipul Jain Guest

    Can any one please tell me what is the difference between global
    scope of an variable and file scope of an variable.

    Vipul
     
    Vipul Jain, Sep 25, 2004
    #1
    1. Advertising

  2. "Vipul Jain" <> wrote...
    > Can any one please tell me what is the difference between global
    > scope of an variable and file scope of an variable.


    None whatsoever.
     
    Victor Bazarov, Sep 26, 2004
    #2
    1. Advertising

  3. Vipul Jain wrote:

    > Can any one please tell me what is the difference
    > between global scope and file scope of an variable.


    > cat file.cc

    int a = 0; // global scope
    static
    int b = 0; // file scope
     
    E. Robert Tisdale, Sep 26, 2004
    #3
  4. Vipul Jain

    Unforgiven Guest

    "E. Robert Tisdale" <> wrote in message
    news:cj553b$43m$...
    > Vipul Jain wrote:
    >
    >> Can any one please tell me what is the difference between global scope
    >> and file scope of an variable.

    >
    > > cat file.cc

    > int a = 0; // global scope
    > static
    > int b = 0; // file scope


    I don't agree with that. The way I was tought it scope deals only with
    visibility of a name in code. Without extra help (an extern declaration),
    "a" is visible only within the translation unit file.cc, so it has file
    scope. As such, true global scope doesn't exist in C++, and the term global
    scope is frequently used to refer to file scope. So as Victor said, there is
    no difference.

    According to the terminology I know, "a" has external linkage and "b" has
    internal linkage, but they both have the same scope (file) and lifetime
    (application).

    --
    Unforgiven
     
    Unforgiven, Sep 26, 2004
    #4
  5. Vipul Jain

    David Harmon Guest

    On Sat, 25 Sep 2004 18:13:16 -0700 in comp.lang.c++, "E. Robert Tisdale"
    <> wrote,
    >Vipul Jain wrote:
    >
    >> Can any one please tell me what is the difference
    >> between global scope and file scope of an variable.

    >
    > > cat file.cc

    > int a = 0; // global scope
    > static
    > int b = 0; // file scope


    Sorry, there is no difference in scope between those two. The names
    "a" and "b" are both introduced into the scope in which the declarations
    occur.

    In a namespace scope (including the global namespace scope), those
    declarations illustrate the difference between internal linkage and
    external linkage.

    In a block scope, those declarations illustrate the difference between
    automatic storage duration and static storage duration.

    I think Victor had the correct answer.
     
    David Harmon, Sep 26, 2004
    #5
  6. David Harmon wrote:

    > E. Robert Tisdale wrote:
    >
    >>Vipul Jain wrote:
    >>
    >>>Can any one please tell me what is the difference
    >>>between global scope and file scope of an variable.

    >>
    >> > cat file.cc

    >> int a = 0; // global scope
    >> static
    >> int b = 0; // file scope

    >
    >
    > Sorry, there is no difference in scope between those two. The names
    > "a" and "b" are both introduced into the scope in which the declarations
    > occur.
    >
    > In a namespace scope (including the global namespace scope), those
    > declarations illustrate the difference between internal linkage and
    > external linkage.
    >
    > In a block scope, those declarations illustrate the difference between
    > automatic storage duration and static storage duration.
    >
    > I think Victor had the correct answer.


    I'm sure that you and Victor have the correct answer to some question
    but I don't think that was the distinction
    that Vipul Jain was looking for.
     
    E. Robert Tisdale, Sep 26, 2004
    #6
  7. "E. Robert Tisdale" <> wrote...
    > David Harmon wrote:
    >
    >> E. Robert Tisdale wrote:
    >>
    >>>Vipul Jain wrote:
    >>>
    >>>>Can any one please tell me what is the difference between global scope
    >>>>and file scope of an variable.
    >>>
    >>> > cat file.cc
    >>> int a = 0; // global scope
    >>> static
    >>> int b = 0; // file scope

    >>
    >>
    >> Sorry, there is no difference in scope between those two. The names "a"
    >> and "b" are both introduced into the scope in which the declarations
    >> occur.
    >>
    >> In a namespace scope (including the global namespace scope), those
    >> declarations illustrate the difference between internal linkage and
    >> external linkage.
    >>
    >> In a block scope, those declarations illustrate the difference between
    >> automatic storage duration and static storage duration.
    >>
    >> I think Victor had the correct answer.

    >
    > I'm sure that you and Victor have the correct answer to some question
    > but I don't think that was the distinction
    > that Vipul Jain was looking for.


    I suppose you have a working crystal ball then, since you definitely knew
    that the OP wanted to know about linkage and not scope. I just can't
    imagine that it was you who confused them...

    V
     
    Victor Bazarov, Sep 27, 2004
    #7
  8. Victor Bazarov wrote:

    > I suppose you have a working crystal ball then,
    > since you definitely knew that
    > the OP wanted to know about linkage and not scope.


    I knew no such thing.
    But I do know what C and C++ programmers usually mean
    when they say "global scope" and I think that you do too.

    > I just can't imagine that it was you who confused them...


    Oh, let's just pretend that I'm confused and *not* Vipul Jain.
    Please elaborate for *me*
    the difference between scope and linkage.
     
    E. Robert Tisdale, Sep 27, 2004
    #8
  9. "E. Robert Tisdale" <> wrote...
    > Please elaborate for *me*
    > the difference between scope and linkage.


    Get a copy of the Standard and study it.
     
    Victor Bazarov, Sep 27, 2004
    #9
  10. Vipul Jain

    David Harmon Guest

    On Sun, 26 Sep 2004 18:27:57 -0700 in comp.lang.c++, "E. Robert Tisdale"
    <> wrote,

    >Please elaborate for *me* the difference between scope and linkage.


    An identifier that can be referred to as ::identifier is in the global
    scope (AKA global namespace scope.) You have already shown how such an
    identifier can have internal or external linkage.
     
    David Harmon, Sep 27, 2004
    #10
    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. Paul Opal
    Replies:
    12
    Views:
    1,002
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    694
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    517
  4. Xah Lee
    Replies:
    0
    Views:
    2,282
    Xah Lee
    Feb 26, 2009
  5. Replies:
    0
    Views:
    183
Loading...

Share This Page