473,320 Members | 1,707 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,320 software developers and data experts.

scope and linkage rule, very confusing!

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!
Nov 14 '05 #1
5 2058
On 25 May 2004 21:26:36 -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:

function scope
file scope
block scope
function-prototype scope

I think(might be wrong):

file scope: variables declared outside of any functions.
Correct.
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?
Correct.
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?
Correct. C does ignore these. You can have more than one prototype
for the same function, as long as they all match the type of the
function. Each one can give the parameters different names. And the
actual definition of the function can use different names from any of
the prototypes.

The only use for parameter names in function prototypes is to provide
useful hints to the programmer reading the prototype. Consider the
difference between:

void rectangle(int, int);

....and:

void circle(int height, int width);
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:
This is mixing up two things, and the result is incorrect. There are
two types of labels:

1. Labels that can be the targets of a goto statement, of the form
"some_legal_c_name:". These have function scope. You can "goto" a
label from anywhere in the function that contains the label, even if
the goto statement is before the label definition.

2. Labels that can only appear in a switch statement, namely
"default:" and "case some_integer_constant_expression:". These labels
do not have function scope, they are scoped to the switch block that
contains them. A switch statement can only have one "default:" label,
but there can be more than one switch statement in a function and each
of them can have its own "default:". The same is true for the values
of the expression in the case labels. The value of the expression in
each case label within a single switch must all be different, but more
than one switch in a single function can use the same case values.
int h(int i){
again:
if(i == 0)
goto again;
}

does 'again' have function scope? I am confuse. Can someone help me
out?
Yes, again has function scope. But consider this:

int h(int i)
{
switch (i)
{
case 1: do_something(); break;
case 2: do_something_else(); break;
default: break;
}

switch (i - 1)
{
case 1: do_nothing(); break;
case 2: do_even_less(); break;
default: break;
}
return i;
}

So you can see that each "case 1:", "case 2:", and "default:" label is
scoped to its own switch.
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!


Every identifier in a C program has four primary characteristics:

1. Scope, covered above.

2. Linkage.

3. Storage duration.

4. Name space.

The static keyword is overloaded with two different uses related to
both linkage and storage duration.

The three types of linkage are external, internal, and none. Objects
defined inside of any block have no linkage. Objects defined at file
scope (variables and functions) have external linkage by default.
That is, they may be accessed by name from code in other source code
files compiled separately.

If an object or function defined at file scope has the static keyword
added to its definition, its linkage is changed from external to
internal. Its name is not visible to other source files, and they
cannot refer to it by name.

So if you define the following at file scope:

int x;
int func(int param)
{
return x + param;
}

Then "x" and "func" have external linkage. Code in other source files
can read or write "x" and call "func" so long as those other files
have proper declarations of them.

On the other hand, if you have this at file scope:

static int x;
static int func(int param)
{
return x + param;
}

Then "x" and "func" have internal linkage. They cannot be accessed or
called by name from other source files. Other source files can have
an object named "x" and a function named "func", and they will be
different from these.

Going back to the first example, where "x" and "func" have external
linkage because they are defined without "static", they can be used by
name from other source files, but only if those other source files
know about them.

If another source file wants to access "x", it needs a declaration
like this:

extern int x;

This tells the compiler that "x" will exist somewhere else in the
program, so this declaration does not define another "x", just allows
this function the ability to access it when it is defined somewhere
else.

You can also use the "extern" keyword on function prototypes, but it
is not really necessary because function prototypes, like function
definitions, have external linkage by default.

The third category is storage duration, and there are three types,
static, automatic, and allocated.

All objects defined at file scope have static storage duration. They
exist and have an initial value before main() begins execution and
continue to exist until the program ends.

All objects defined at block scope with no storage class keyword, or
with the keywords "auto" or "register" have automatic storage
duration. They come into existence when execution enters their block.
They go out of existence when execution leaves the block. If they are
not specifically initialized when declared, they have indeterminate
values.

But objects defined within a block using the "static" keyword have
static storage duration. Like file scope objects, they exist and have
an initial value before main() starts and last for the entire program.
They keep their current value even when execution leaves their block.

The third storage duration, allocated, applies to memory obtained from
malloc(), calloc(), or realloc(). It persists until it is free'd by
another call to realloc() or free().

--
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
Nov 14 '05 #2

"Jack Klein" <ja*******@spamcop.net> wrote in message news:ua********************************@4ax.com...
On 25 May 2004 21:26:36 -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:

function scope
file scope
block scope
function-prototype scope

I think(might be wrong):

file scope: variables declared outside of any functions.


Correct.
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?


Correct.
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?


Correct. C does ignore these. You can have more than one prototype
for the same function, as long as they all match the type of the
function. Each one can give the parameters different names. And the
actual definition of the function can use different names from any of
the prototypes.

The only use for parameter names in function prototypes is to provide
useful hints to the programmer reading the prototype. Consider the
difference between:

void rectangle(int, int);

...and:

void circle(int height, int width);
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:


This is mixing up two things, and the result is incorrect. There are
two types of labels:

1. Labels that can be the targets of a goto statement, of the form
"some_legal_c_name:". These have function scope. You can "goto" a
label from anywhere in the function that contains the label, even if
the goto statement is before the label definition.

2. Labels that can only appear in a switch statement, namely
"default:" and "case some_integer_constant_expression:". These labels
do not have function scope, they are scoped to the switch block that
contains them. A switch statement can only have one "default:" label,
but there can be more than one switch statement in a function and each
of them can have its own "default:". The same is true for the values
of the expression in the case labels. The value of the expression in
each case label within a single switch must all be different, but more
than one switch in a single function can use the same case values.
int h(int i){
again:
if(i == 0)
goto again;
}

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


Yes, again has function scope. But consider this:

int h(int i)
{
switch (i)
{
case 1: do_something(); break;
case 2: do_something_else(); break;
default: break;
}

switch (i - 1)
{
case 1: do_nothing(); break;
case 2: do_even_less(); break;
default: break;
}
return i;
}

So you can see that each "case 1:", "case 2:", and "default:" label is
scoped to its own switch.
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!


Every identifier in a C program has four primary characteristics:

1. Scope, covered above.

2. Linkage.

3. Storage duration.

4. Name space.

The static keyword is overloaded with two different uses related to
both linkage and storage duration.

The three types of linkage are external, internal, and none. Objects
defined inside of any block have no linkage. Objects defined at file
scope (variables and functions) have external linkage by default.
That is, they may be accessed by name from code in other source code
files compiled separately.

If an object or function defined at file scope has the static keyword
added to its definition, its linkage is changed from external to
internal. Its name is not visible to other source files, and they
cannot refer to it by name.

So if you define the following at file scope:

int x;
int func(int param)
{
return x + param;
}

Then "x" and "func" have external linkage. Code in other source files
can read or write "x" and call "func" so long as those other files
have proper declarations of them.

On the other hand, if you have this at file scope:

static int x;
static int func(int param)
{
return x + param;
}

Then "x" and "func" have internal linkage. They cannot be accessed or
called by name from other source files. Other source files can have
an object named "x" and a function named "func", and they will be
different from these.

Going back to the first example, where "x" and "func" have external
linkage because they are defined without "static", they can be used by
name from other source files, but only if those other source files
know about them.

If another source file wants to access "x", it needs a declaration
like this:

extern int x;

This tells the compiler that "x" will exist somewhere else in the
program, so this declaration does not define another "x", just allows
this function the ability to access it when it is defined somewhere
else.

You can also use the "extern" keyword on function prototypes, but it
is not really necessary because function prototypes, like function
definitions, have external linkage by default.

The third category is storage duration, and there are three types,
static, automatic, and allocated.

All objects defined at file scope have static storage duration. They
exist and have an initial value before main() begins execution and
continue to exist until the program ends.

All objects defined at block scope with no storage class keyword, or
with the keywords "auto" or "register" have automatic storage
duration. They come into existence when execution enters their block.
They go out of existence when execution leaves the block. If they are
not specifically initialized when declared, they have indeterminate
values.

But objects defined within a block using the "static" keyword have
static storage duration. Like file scope objects, they exist and have
an initial value before main() starts and last for the entire program.
They keep their current value even when execution leaves their block.

The third storage duration, allocated, applies to memory obtained from
malloc(), calloc(), or realloc(). It persists until it is free'd by
another call to realloc() or free().


The structure/enum tags are not covered. Do they have namespace
or scope? What is the difference between these two, then?

Vijay
Nov 14 '05 #3
"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
I think(might be wrong):

file scope: variables declared outside of any functions.
Right.
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?
Maybe I'm wrong, but I suppose that's function scope. Or function body
is treated as a block?
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?

I suppose yes.
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?
Well, can someone help me out too? :-)
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 used for declaring the variable, that's declared in another file
(as far as that variable should be allocated in one and the same place).
That's used to share variables among multiple files of program.
Nov 14 '05 #4
On Wed, 26 May 2004 12:44:31 +0600,
I. Appel <ia****@rol.ru> wrote:

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?


Well, can someone help me out too? :-)


Labels found in an inner block is visible in the entire function. Normal
variables are only visible from the point of definition or declaration to
the end of the current block. Example: (of bad usage of goto).

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
if (0) {
label:
printf("this is a test\n");
return 0;
}
goto label;
}
--
Villy
Nov 14 '05 #5
On Wed, 26 May 2004 12:26:28 +0530, "Vijay Kumar R Zanvar"
<vi*****@globaledgesoft.com> wrote in comp.lang.c:

[snip]
Every identifier in a C program has four primary characteristics:

1. Scope, covered above.

2. Linkage.

3. Storage duration.

4. Name space.

[snip]
The structure/enum tags are not covered. Do they have namespace
or scope? What is the difference between these two, then?

Vijay


It was less than a year ago that I was able to get broadband at home.
Before then I would have been extremely annoyed at someone who quoted
an entire 200+ line post to add a two-line question at the end. The
message download time for me is no longer an issue, so I will merely
point out that over quoting is still considered bad manners. I think
there are still some newsreaders that will not post a message if the
quoted to new material ratio is too high. You could have snipped as
much from my post as I did.

I thought a little about whether or not to include name space in the
list, as the OP hadn't asked about it and I didn't plan to address it.
On the other hand, if I left it off the list and said there were only
three characteristics, somebody was bound to complain that the C
standard clearly mentions four, and I had forgotten one.

So I did list it, and since you asked...

C defines four types of name space, and no symbol can have two
different definitions in the same scope and in the same name space.
On the other hand, there can be many instances of the same symbol or
identifier in scope if each is in a different name space.

Here they are:

1. Labels (goto labels, that is). They are always easily detectable
by the translator because they only appear correctly in two
circumstances, either followed by a ':' or between the 'goto' keyword
and a semicolon.

2. Struct, union and enum tags. They are easily detected by the fact
that they immediately follow the 'struct', 'union', or 'enum' keyword
in both declaration and instantiation.

3. Each structure and union definition creates its own name space for
the names of its members. They are likewise easily recognized because
they must appear in connection with struct_type_ptr-> or
struct_object.

4. All other identifiers, including those created with typedef.

This allows, for example, either of:

struct name_space /* tag name space */
{
int name_space; /* member name space */
} name_space; /* ordinary name space */

....or:

typedef struct name_space /* tag name space */
{
int name_space; /* member name space */
} name_space; /* ordinary name space */

....but now you can't define an instance of the struct like this:

struct name_space name_space;

....or:

name_space name_space;

....because the typedef name is in the same name space as object names.

Tag and member names are in scope wherever the struct/union/enum
definition is in scope. Like labels, tags and member names have no
linkage.

--
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
Nov 14 '05 #6

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

Similar topics

6
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...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
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
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; //...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.