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! 6 2990
* pe********@yahoo.com (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?
On 25 May 2004 21:27:31 -0700, pe********@yahoo.com (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/~a...FAQ-acllc.html
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 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.
Alf P. Steinbach wrote: * pe********@yahoo.com (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
Jack Klein <ja*******@spamcop.net> wrote in message news:<vn********************************@4ax.com>. .. On 25 May 2004 21:27:31 -0700, pe********@yahoo.com (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! This discussion thread is closed Replies have been disabled for this discussion. Similar topics
9 posts
views
Thread by Vipul Jain |
last post: by
|
5 posts
views
Thread by pembed2003 |
last post: by
|
4 posts
views
Thread by Peter Ammon |
last post: by
|
8 posts
views
Thread by TTroy |
last post: by
|
7 posts
views
Thread by Kobu |
last post: by
|
6 posts
views
Thread by Neelesh Bodas |
last post: by
|
7 posts
views
Thread by Christian Christmann |
last post: by
|
3 posts
views
Thread by parag_paul |
last post: by
|
1 post
views
Thread by Giacomo Catenazzi |
last post: by
| | | | | | | | | | | |