Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > External variable question

Reply
Thread Tools

External variable question

 
 
David RF
Guest
Posts: n/a
 
      02-11-2011
On 11 feb, 15:57, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> David RF <davran...@gmail.com> writes:
> > On 11 feb, 10:51, Mateusz_madi <madi.cz...@gmail.com> wrote:
> >> On 11 Lut, 10:17, David RF <davran...@gmail.com> wrote:
> >> > On 11 feb, 09:58, Mateusz_madi <madi.cz...@gmail.com> wrote:

> <snip>
> >> > > a.c
> >> > > ---------
> >> > > #include<stdio.h>

>
> >> > > int a=3;

>
> >> > > void drukuj(){
> >> > > Â* Â* Â* Â* printf("%d", a);

>
> >> > > }

>
> >> > > -----------
> >> > > b.c
> >> > > -----------

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

>
> >> > > int a;

>
> >> > > int main()
> >> > > {
> >> > > Â* Â* Â* Â* a=8;
> >> > > Â* Â* Â* Â* drukuj();}

>
> >> > > ---------

>
> >> > > Why it prints 8 not 3.

>
> >> > Because a have global scope

>
> >> > Declare a as static in file b.c
> >> > static int a;- Ukryj cytowany tekst -

>
> >> > - Pokaż cytowany tekst -

>
> >> There is something i don't understand about global variables. I found
> >> in many pages that:
> >> Â*"Global - All the modules in the project - Â*The life of the program
> >> execution "
> >> buth if i declare global let's say g in one module and would like to
> >> increment it in second module it is impossible, so where is that
> >> global (All the modules in the project) scope?

>
> > Ok, I try to explain with my poor english

>
> > both a in a.c and b.c point to same location and have global scope, a
> > in b.c is redeclared, all variables declared without static duration
> > outside a function have global scope

>
> That's not really the problem. Â*The program is undefined because it has
> two external definitions of an object with the same name.
>
> Unfortunately your terms are rather confused. Â*Normally this does not
> matter, but in this case using the right terms helps to get everything
> clear.
>
> In C, a named object is characterised by three properties: the scope of
> the name, the lifetime of the object and the linkage of the name. Â*These
> are related but also to some extent independent.
>
> An external definition of an object (one that sits outside of any
> function) gives it static duration -- even if it does not use the word
> static! Â*If an external declaration does use the keyword "static", this
> alters the linkage but not the lifetime.
>
> C does not have global variables in the same sense that some other
> languages do. Â*Normally, when people talk about "glabals" in C they mean
> objects with external linkage, static duration and file scope. Â*Using the
> term "global" (or "global scope") is rarely a problem, but in this case
> it lies at the heart of the OP's confusion so I think it pays to use the
> right language. Â*Here are some examples:
>
> ----------------
> int a = 3; /* The name has file scope and external linkage.
> Â* Â* Â* Â* Â* Â* Â* The object named has static duration */
>
> static int b = 42; /* The name has file scope but internal linkage.
> Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* The object has static duration */
>
> void f(int a) /* f has file scope and external linkage but functions
> Â* Â* Â* Â* Â* Â* Â* Â* Â*don't have any storage duration.
> Â* Â* Â* Â* Â* Â* Â* Â* Â*a has block scope, no linkage and automatic duration. */
> {
> Â* Â* int b; /* block scope, no linkage, automatic duration. */
> Â* Â* static int c; /* block scope, internal linkage and static duration */
> Â* Â* extern int d; /* block scope, external linkage and static duration */
>
> }
>
> void g(int x); /* x has function prototype scope, no linkage and
> Â* Â* Â* Â* Â* Â* Â* Â* Â* (insofar as it makes any sense) automatic duration. */
>
> int t; /* A tentative definition of a name with file scope and external
> Â* Â* Â* Â* Â* linkage naming an object with static duration. *.
>
> int t2; /* Another tentative definition. */
>
> int t2 = 42; /* Not a re-definition since the previous one was
> Â* Â* Â* Â* Â* Â* Â* Â* tentative. */
>
> /* Here, at the end of the translation unit, the tentative definition of
> Â* Â*'t' is treated as if it had been "int t = 0;". Â*I.e. the tentative
> Â* Â*definition becomes a real one. */
> ----------------
>
> There are two rather curious details. Â*An object with automatic duration
> that is not a variably modified array exists from entry into the block
> until the block is exited[1]. Â*A variably modified array only exists
> from the point of it's definition:
>
> Â* int peculiar(unsigned int s)
> Â* {
> Â* Â* Â* Â*int *ip = 0;
> Â* top: if (ip) return *ip;
> Â* Â* Â* Â*int a[1];
> Â* Â* Â* Â*a[0] = 42;
> Â* Â* Â* Â*ip = a;
> Â* Â* Â* Â*goto top;
> Â* }
>
> is well-defined but if 'a' were changed to be "int a[s];" then the
> behaviour would be undefined.
>
> [1] Even this needs care to define properly but I'll leave it as
> self-evident for the moment.


Thanks for the correction
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Evaluate an string variable's value to a variable Mir Nazim Python 2 12-21-2008 07:05 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
unresolved external symbol/using an external dll Scott Allen C++ 8 05-02-2004 06:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57