scope, static and linkage

P

pembed2003

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!
 
A

Alf P. Steinbach

* (e-mail address removed) (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.
 
J

Jack Klein

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

Erdal MUTLU

Jack said:
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
 
J

Jeff Schwab

Erdal said:
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.
 
J

Jorge Rivera

Alf said:
* (e-mail address removed) (pembed2003) schriebt:

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
 
P

pembed2003

Jack Klein said:
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!
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top