Accessing vars in global and module scope in ANSI C 
Author Message
 Accessing vars in global and module scope in ANSI C

The following program contains four variables using the same name and at
different levels of scope:

extern int x;    // Global scope        
static int x;    // Module scope

void main (void)
{
    int x;       // Function scope

    {
        int x;   // Block scope
    }

Quote:
}

Is there any way to access the extern or static variables from within the
function or block? Nothing in the FAQ about this...

--

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"



Sat, 04 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C
Please post valid C code in this newsgroup.  // comments are syntax
errors in C and main() is always returning int -- please read the FAQ
before posting.


Quote:
>The following program contains four variables using the same name and at
>different levels of scope:

>extern int x;    // Global scope    
>static int x;    // Module scope

These declarations are conflicting, because there is no way to
disambiguate between the extern x and the static x.
Quote:

>void main (void)
>{
>    int x;       // Function scope

>    {
>    int x;   // Block scope
>    }
>}

>Is there any way to access the extern or static variables from within the
>function or block? Nothing in the FAQ about this...

You cannot do this.  At one time, you can only "see" one x.  The most
you can do is:

extern int x;           /* Global scope */

main()
{
    int x;              /* Function scope */

    {
        extern int x;   /* This is the one with global scope again */
    }

Quote:
}

The FAQ doesn't cover this subject because it's extremely silly to have
more than one variable with the same name, especially if you intend to
maintain your programs.

And because this is not a frequently asked question :-)

If you want to access an x which is overriden by another one, the only
possibility is a pointer.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sat, 04 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C
: Please post valid C code in this newsgroup.  // comments are syntax
: errors in C and main() is always returning int -- please read the FAQ
: before posting.

I am using Microsoft C 8.0 and it has no problems with using single line
comments or declaring main() with a return type of "void".

: >
: >extern int x;    // Global scope  
: >static int x;    // Module scope

: These declarations are conflicting, because there is no way to
: disambiguate between the extern x and the static x.
:
MSC 8.0 does not have a problem with this. Assume "int x" is also declared as
a global in another module for the extern. Each "x" is a separate, unambiguous
variable. I was interested in knowing if there was a way(s) to reference
each separately in the same module.

: The FAQ doesn't cover this subject because it's extremely silly to have
: more than one variable with the same name, especially if you intend to
: maintain your programs.
:
This is purely an acedemic question with regards to the way C implements
the concept of "scope". Very few books cover the concept of scope and
modularity in C. Perhaps this is an example for the next edition of Koenig's
CT&P book

: If you want to access an x which is overriden by another one, the only
: possibility is a pointer.
:
Thank you. You could have just said this in the first place and saved me
a rebuttal.

--

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"



Sat, 04 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:

>: Please post valid C code in this newsgroup.  // comments are syntax
>: errors in C and main() is always returning int -- please read the FAQ
>: before posting.

>I am using Microsoft C 8.0 and it has no problems with using single line
>comments or declaring main() with a return type of "void".

The name of this newsgroup comp.lang.c and not comp.lang.msc-8.
From a practical point of view, if somebody else wants to cut and paste
your example, to test it on his compiler, he'll have to do also a lot
of editing, to remove your comments.  This is lack of respect for your
intended audience.

Quote:

>: >
>: >extern int x;    // Global scope      
>: >static int x;    // Module scope

>: These declarations are conflicting, because there is no way to
>: disambiguate between the extern x and the static x.
>:
>MSC 8.0 does not have a problem with this. Assume "int x" is also declared as
>a global in another module for the extern. Each "x" is a separate, unambiguous
>variable. I was interested in knowing if there was a way(s) to reference
>each separately in the same module.

I thought your question was about C and not about what your compiler is
doing.  So, my comment was about the C language.

If ``each "x" is a separate, unambiguous variable'', how does MSC 8.0
figure out which want you want to access?  Is it reading your mind?
It seems that MS has made considerable advances in compiler technology :-)

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 05 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:

>>: >
>>: >extern int x;    // Global scope    
>>: >static int x;    // Module scope

>>: These declarations are conflicting, because there is no way to
>>: disambiguate between the extern x and the static x.
>>:
>>MSC 8.0 does not have a problem with this. Assume "int x" is also
declared as
>>a global in another module for the extern. Each "x" is a separate,
unambiguous
>>variable. I was interested in knowing if there was a way(s) to
reference
>>each separately in the same module.

>I thought your question was about C and not about what your compiler is
>doing.  So, my comment was about the C language.

>If ``each "x" is a separate, unambiguous variable'', how does MSC 8.0
>figure out which want you want to access?  Is it reading your mind?
>It seems that MS has made considerable advances in compiler technology
:-)

>Dan
>--

He is probably compiling with the C++ options on, so the compiler is
'name mangling'.  I'm not familiar with its affect on variable names
with different scope, but it may allow it. In C++ using the global
scope operator would allow him to access the global version.
{
int x;
x = ::x;
Quote:
}

but I don't know, if that would be his global or the module version.  
I'm afraid I don't see any reason for having both, but I'm don't know
the specs for his app.

I believe this discussion should probably move to c.l.c++.  

Later

jatmon



Sun, 05 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

: The name of this newsgroup comp.lang.c and not comp.lang.msc-8.
: From a practical point of view, if somebody else wants to cut and paste
: your example, to test it on his compiler, he'll have to do also a lot
: of editing, to remove your comments.  This is lack of respect for your
: intended audience.
:
No fear. Nobody will cut-and-paste my example except you because you are
the only person reading this newsgroup that can't seem to realize this
code is academic and not practical.

: I thought your question was about C and not about what your compiler is
: doing.  So, my comment was about the C language.

My question is about ANSI C. MSC 8.0 is supposedly ANSI C compliant (albeit
with a few extensions, such as single line comments).

: If ``each "x" is a separate, unambiguous variable'', how does MSC 8.0
: figure out which want you want to access?  Is it reading your mind?
: It seems that MS has made considerable advances in compiler technology :-)

That's what I want to know. So far I have come to the conclusion that
identical names make it impossible to access some variables at certain levels
of scope. Only by "renaming" the variables via a pointer can they be accessed.

And Mr. Pop:

Thank you for your kind words of support, the likes of which have truly been
the foundation and support of the humanistic part of the Internet community.

--

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"



Sun, 05 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

: Nope, it can't be done without using a pointer pointing to the
: global variables (static or external linkage). But, if you do have
: the sources available, why not change the names of those local
: variables instead? Having identical names for local variables
: _and_ wanting to access global variables with the same name is
: quite unreadable I might say ...

This is purely an academic example of scoping rules in ANSI C. It is not
ment as a suggestion as to an implementation methodology people should
use for their variable naming conventions.

: ps. Another possibilty is switching to C++ and use the scope
:     resolution operator `::'

I am specifically interested about scope in ANSI C and not C++ (at this time).

Thanks much.

--

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"



Sun, 05 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

: He is probably compiling with the C++ options on, so the compiler is
: 'name mangling'.  I'm not familiar with its affect on variable names
: with different scope, but it may allow it.

Nope. No C++ extension "turned on". And name mangling applies to functions
and not variable names (not that I've ever read, anyway).

: In C++ using the global
: scope operator would allow him to access the global version.
: {
: int x;
: x = ::x;
: }

That's great, but my question is specifically about ANSI C, and not C++.

: but I don't know, if that would be his global or the module version.  
: I'm afraid I don't see any reason for having both, but I'm don't know
: the specs for his app.

No specs. This is an academic and not practical question.

: I believe this discussion should probably move to c.l.c++.  

No, it shouldn't!

: Later

Much. And thanks...

--

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"



Sun, 05 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:
>: I thought your question was about C and not about what your compiler is
>: doing.  So, my comment was about the C language.

>My question is about ANSI C. MSC 8.0 is supposedly ANSI C compliant (albeit
>with a few extensions, such as single line comments).

A compiler may implement extensions to the language where the standard
leaves the behaviour undefined. Since undefined behaviour means the
compiler may do anything, anything it does is conforming.

However // style comments are not a case of undefined behaviour, rather
they are a syntax error. An ANSI conforming compiler is *required* by the
standard to generate a diagnostic (e.g. a warning message) in such a case.
It couls still go on to compile the program as you exptected, the important
thing is the diagnostic. If your compiler doesn't generate a diagnostic
it is not an ANSI conformat compiler (at least not using the compiler
options you did, that might change with different compiler options).

A rather dim view of // comments (and other C++ extensions) is taken in
comp.lang.c because:

a) they are not C

b) they confuse the reader about the environment you're really using. Not
   everybody uses or has ever used MSC 8.0 but if you write in standard C
   they won't need to know anything about your compiler to understand your
   code.

c) they cause problems for somebody trying to test your code by compiling it
   on a real C compiler (which is not unreasonable for somebody reading
   comp.lang.c).

If you yourself intend to, write C code (as opposed to C++ or any other code)
you're much better off sticking to what is defined by the C language since
you know this will continue to work if you change compilers or even different
versions of the same compiler.

--
-----------------------------------------


-----------------------------------------



Sun, 05 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:
> extern int x;    // Global scope  
> static int x;    // Module scope
>     int x;       // Function scope
>    int x;   // Block scope

    ...

Quote:
> Is there any way to access the extern or static variables from within the
> function or block? Nothing in the FAQ about this...

    By far the easiest way to do this is to rename the variables in
    the different scopes, as there is no general portable way to do
    this in C.

    One hard method, however, might involve some assembler and knowing
    that the outer variables are accessed relative to different
    positions (eg, a4, pc, a5, maybe, on a 680x0).

    Another hard method might use some offset relative to other
    variables. For example, if the global scope x follows a global
    scope y in memory, you might be able to do something like...

        *(&y + 1) = ...

Quote:
> James D. Murray  POB 70                    Tel: 714.288.0141

--

***        "Puppet Boy, it's the little things that count" - DEVO       ***


Mon, 06 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

: However // style comments are not a case of undefined behaviour, rather
: they are a syntax error. An ANSI conforming compiler is *required* by the
: standard to generate a diagnostic (e.g. a warning message) in such a case.

Perhaphs at a higher warning level Microsoft's compiler generates a
dianostic message. I've never tried it.

: A rather dim view of // comments (and other C++ extensions) is taken in
: comp.lang.c because:

Ummmm...thank for the sermon on C++ comments, but take it off my thread.
I am only interested in responses to a scoping situation under ANSI C.
Your soapbox lies elsewhere.

--

James D. Murray  POB 70                 Tel: 714.288.0141

Maintainer of the Graphics File Formats FAQ on comp.graphics and
coauthor of the O'Reilly book "Encyclopedia of Graphics File Formats".

"I'm not only an INTJ, I'm smug about it!"



Mon, 06 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

|
|: Nope, it can't be done without using a pointer pointing to the
|: global variables (static or external linkage). But, if you do have
|: the sources available, why not change the names of those local
|: variables instead? Having identical names for local variables
|: _and_ wanting to access global variables with the same name is
|: quite unreadable I might say ...

|This is purely an academic example of scoping rules in ANSI C. It is not
|ment as a suggestion as to an implementation methodology people should
|use for their variable naming conventions.

Ok, academic examples and questions are easy ;-) Have a look at the Standard:

6.1.2.1 Scope of identifiers

`If an outer declaration of a lexically identical identifier exists
in the same name space, it is hidden until the current scope terminates,
after which it again becomes visible.'

This is just a formal way of saying that:

        int x;

        int foo() {

        int x;

        /* the global variable `x' cannot be accessed
         * within this function directly
         */

         }

I guess this about says it all ...

kind regards,




Mon, 06 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:

>Nope, it can't be done without using a pointer pointing to the
>global variables (static or external linkage). But, if you do have

So,

        {
                int x=1;
                {
                        int *xp= &x;
                        int x= 2;

                        printf("%d", *xp);
                }
        }

Is this guaranteed to print 1? Like, do things come into scope
instantly at the start of the block, or as they are declared?
I suspect it is as declared and this "works".

A
A
A
A
A
A
A
A
A
A
A
A
A

--



Tue, 07 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:
Krell) writes:

<snip>
|>   {
|>           int x=1;
|>           {
|>                   int *xp= &x;
|>                   int x= 2;
|>
|>                   printf("%d", *xp);
|>           }
|>   }
|>
|> Is this guaranteed to print 1? Like, do things come into scope
|> instantly at the start of the block, or as they are declared?
|> I suspect it is as declared and this "works".

Your suspicion is correct. The program fragment above does what you probably
want it to do.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Tue, 07 Oct 1997 03:00:00 GMT  
 Accessing vars in global and module scope in ANSI C

Quote:

> : Please post valid C code in this newsgroup.  // comments are syntax
> : errors in C and main() is always returning int -- please read the FAQ
> : before posting.

> I am using Microsoft C 8.0 and it has no problems with using single line
> comments or declaring main() with a return type of "void".

As Dan said, your "single line comments" are syntax errors in C.  If
MSC 8.0 accepts them, then it's not a C compiler.  Hopefully, there is a
switch that you have set incorrectly that, when set correctly, makes it
a C compiler.  Declaring main() with a return type of void produces
undefined behavior; the fact that it happens to work on many compilers
is irrelevent.

Quote:
> : >extern int x;    // Global scope    
> : >static int x;    // Module scope

> : These declarations are conflicting, because there is no way to
> : disambiguate between the extern x and the static x.
> :
> MSC 8.0 does not have a problem with this. Assume "int x" is also declared as
> a global in another module for the extern. Each "x" is a separate, unambiguous
> variable. I was interested in knowing if there was a way(s) to reference
> each separately in the same module.

Again, this is undefined behavior.  ISO 6.1.2.2 says "If, within a
translation unit, the same identifier appears with both internal and
external linkage, the behavior is undefined."  Since the behavior is
undefined, absolutely anything can happen.  Each "x" is NOT a separate,
unambiguous variable, rather it's a single variable that you're trying
to have behave in mututally conflicting ways -- a compiler might resolve
the conflict by creating two separate variables, or it might create a
single variable and ignore one of the behaviors, or it might refuse to
compile the program, or it might turn into a butterfly and fly away.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070

I don't think math is a science, I think it's a religion. -- Calvin


Tue, 07 Oct 1997 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Accessing vars in global and module scope in ANSI C

2. global vars vs: local vars

3. ANSI C and global variable scope

4. how to access local and global vars in a function

5. Accessing vars in global

6. Module scope variables visible in other Modules?

7. global/static vars inside loops

8. const global vars

9. Global vars versus pointers

10. global vs local vars

11. global/static vars inside loops

12. static global vars and functions

 

 
Powered by phpBB® Forum Software