473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

scope, static and linkage

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!
Jul 22 '05 #1
6 3129
* 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?
Jul 22 '05 #2
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
Jul 22 '05 #3
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

Jul 22 '05 #4
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.
Jul 22 '05 #5
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
Jul 22 '05 #6
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!
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
5
by: pembed2003 | last post by:
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...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
7
by: Kobu | last post by:
The code below isn't compiling for me (error message: conflicting types for 'total' - pointing to the extern declaration). Why wouldn't this work, since the types are different, the extern...
6
by: Neelesh Bodas | last post by:
Hello All, I was just listing down various ways in which variables can be created and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3) Putting the summary here for corrections, comments,...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
3
by: parag_paul | last post by:
hi All for the following #include <stdio.h> typedef struct{ int a ; int b; char* j; } AST;
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.