scope, static and linkage

Discussion in 'C++' started by pembed2003, May 26, 2004.

  1. pembed2003

    pembed2003 Guest

    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
    function-prototype scope

    I think(might be wrong):

    file scope: variables declared outside of any functions.

    block scope: variables declared inside a block such as a function. For
    example:

    int f(int i, int j){
    int k;
    return 0;
    }

    then the variable i, j and k are all block scope, right?

    function-prototype scope: The book said C ignores those. For example:

    int f(int x,int y);

    in this case, x and y are function-prototype scope?

    function scope: This one I don't know. The book said only labels have
    function scope and the 'switch' and 'goto' statements normally uses
    those. For example:

    int h(int i){
    again:
    if(i == 0)
    goto again;
    }

    does 'again' have function scope? I am confuse. Can someone help me
    out?

    Also, What does static and extern use for? I know static can be used
    to make the variable persist after function calls but what about
    exter?

    Thanks!
    pembed2003, May 26, 2004
    #1
    1. Advertising

  2. * (pembed2003) schriebt:
    >
    > I am reading the book "C++ How to Program"


    Haven't heard of it, but ...


    > in the chapter where it
    > discuss scope rule, it says there are four scopes for a variable:
    >
    > function scope
    > file scope
    > block scope
    > function-prototype scope


    .... you should ditch that book.

    Add to the list above, class scope (which is what C++ is all about!),
    and note that the terminology employed by the author is non-standard.

    Get yourself e.g. "Accelerated C++" plus Bjarne's bible "The C++
    Programming Language", and use the latter as a kind of reference.


    > I think(might be wrong):
    >
    > file scope: variables declared outside of any functions.


    C++ has no notion of files for the source code. The standard goes
    through very painful contortions to _avoid_ mentioning files. What
    the author probably means is in Holy Standard speak "namespace scope".

    Assuming that is so, it means variables declared outside any functions
    or classes.


    > block scope: variables declared inside a block such as a function. For
    > example:
    >
    > int f(int i, int j){
    > int k;
    > return 0;
    > }
    >
    > then the variable i, j and k are all block scope, right?


    Presumably that is what the author means.

    The Holy Standard calls this a "local scope".


    > function-prototype scope: The book said C ignores those. For example:
    >
    > int f(int x,int y);
    >
    > in this case, x and y are function-prototype scope?


    Yes.


    > function scope: This one I don't know. The book said only labels have
    > function scope


    Yes.

    But a label is not a variable.

    Perhaps the book was really talking about scopes for _identifiers_?



    > and the 'switch' and 'goto' statements normally uses
    > those. For example:
    >
    > int h(int i){
    > again:
    > if(i == 0)
    > goto again;
    > }
    >
    > does 'again' have function scope?


    Yes. Any label declared in a function is accessible within all
    of the function, regardless of whether it's declared in a block
    or wherever.


    > I am confuse. Can someone help me out?


    That is off-topic in this group.


    > Also, What does static and extern use for? I know static can be used
    > to make the variable persist after function calls but what about
    > exter?


    'extern' is the easier one. It means that something has external
    linkage. In effect, that it can be referenced from another compilation
    unit.

    The meaning of 'static' depends on the context.

    'static' on a variable has the effect you describe. At namespace scope
    it also means the opposite of 'extern', and in that capacity it can be
    applied to variables and functions. However, this latter usage is
    deprecated and imposes a limitation on the usage of a function so
    declared (namely that the function cannot be used as a template
    argument); the new way of achieving "insulation" from other compilation
    units is to use an anonymous namespace.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 26, 2004
    #2
    1. Advertising

  3. pembed2003

    Jack Klein Guest

    On 25 May 2004 21:27:31 -0700, (pembed2003) wrote
    in comp.lang.c++:

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


    [snip]

    Which is it, "C How to Program", as you posted in comp.lang.c, or "C++
    How to Program", as you posted here? Are you pulling the wool over
    our eyes? If you are actually reading both at the same time, that is
    very probably a mistake. Choose one of the languages and learn it
    first, then learn the other if you choose.

    If it is truly a C++ book, either it is old and obsolete or just plain
    wrong.

    I gave a detailed answer, for C, in comp.lang.c. For C++, as Alf
    already pointed out, there is no such thing as "file scope", and there
    are other scopes in standard C++ that the book doesn't mention.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, May 26, 2004
    #3
  4. pembed2003

    Erdal MUTLU Guest

    Jack Klein wrote:

    > I gave a detailed answer, for C, in comp.lang.c. For C++, as Alf
    > already pointed out, there is no such thing as "file scope", and there
    > are other scopes in standard C++ that the book doesn't mention.
    >


    If there is no file scope, what is this then:

    file: foo.cpp
    ....
    static int i;
    ....

    this variable is visible only within this unit. Am I wrong?

    Best regards.
    Erdal Mutlu
    Erdal MUTLU, May 26, 2004
    #4
  5. pembed2003

    Jeff Schwab Guest

    Erdal MUTLU wrote:
    > Jack Klein wrote:
    >
    >> I gave a detailed answer, for C, in comp.lang.c. For C++, as Alf
    >> already pointed out, there is no such thing as "file scope", and there
    >> are other scopes in standard C++ that the book doesn't mention.
    >>

    >
    > If there is no file scope, what is this then:
    >
    > file: foo.cpp
    > ...
    > static int i;
    > ...
    >
    > this variable is visible only within this unit. Am I wrong?


    Depends on what "..." means. If they mean nothing but blank space, the
    variable i is in namespace scope, and has static linkage.
    Jeff Schwab, May 26, 2004
    #5
  6. pembed2003

    Jorge Rivera Guest

    Alf P. Steinbach wrote:
    > * (pembed2003) schriebt:
    >
    >>I am reading the book "C++ How to Program"

    >


    FYI

    It is actually a book by Deitel and Deitel. They have a family of "'X'
    how to program" that includes C,C++,Java aand C# (at least those are the
    ones I know of.

    The book was not bad when I read it 8 years ago (pre-standard era).

    I guess they never updated it after the standard...

    JLR
    Jorge Rivera, May 26, 2004
    #6
  7. pembed2003

    pembed2003 Guest

    Jack Klein <> wrote in message news:<>...
    > On 25 May 2004 21:27:31 -0700, (pembed2003) wrote
    > in comp.lang.c++:
    >
    > > 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:

    >
    > [snip]
    >
    > Which is it, "C How to Program", as you posted in comp.lang.c, or "C++
    > How to Program", as you posted here?
    >


    It's actually both. I have "C How to Program" and "C++ How to
    Program". Actually I think they have a series of those "... How to
    Program" books.

    Both books mention the same scope rules and that's why I am posting to
    both C and C++ newsgroup for information.

    > Are you pulling the wool over
    > our eyes? If you are actually reading both at the same time, that is
    > very probably a mistake. Choose one of the languages and learn it
    > first, then learn the other if you choose.


    Thanks. I try to but since both books mentions the same thing, I
    though they are the same for both languages. A common mistake for
    newbie like me.

    >
    > If it is truly a C++ book, either it is old and obsolete or just plain
    > wrong.
    >


    Base on the title, I think it's a C++ book but you are right that it's
    very old.

    > I gave a detailed answer, for C, in comp.lang.c. For C++, as Alf
    > already pointed out, there is no such thing as "file scope", and there
    > are other scopes in standard C++ that the book doesn't mention.


    Thanks for the info!
    pembed2003, May 27, 2004
    #7
    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:
    928
    Paul Opal
    Oct 11, 2004
  2. pembed2003

    scope and linkage rule, very confusing!

    pembed2003, May 26, 2004, in forum: C Programming
    Replies:
    5
    Views:
    651
    Jack Klein
    May 27, 2004
  3. rick

    scope and linkage

    rick, Mar 31, 2006, in forum: C Programming
    Replies:
    5
    Views:
    352
    Jack Klein
    Apr 1, 2006
  4. tropos
    Replies:
    3
    Views:
    448
  5. Replies:
    1
    Views:
    579
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page