[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

External variable question

Mateusz_madi

2/11/2011 8:59:00 AM

Hi i have 2 modules program:

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.

Regards,
mateusz
12 Answers

David RF

2/11/2011 9:18:00 AM

0

On 11 feb, 09:58, Mateusz_madi <madi.cz...@gmail.com> wrote:
> Hi i have 2 modules program:
>
> 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;

Mateusz_madi

2/11/2011 9:51:00 AM

0

On 11 Lut, 10:17, David RF <davran...@gmail.com> wrote:
> On 11 feb, 09:58, Mateusz_madi <madi.cz...@gmail.com> wrote:
>
>
>
>
>
> > Hi i have 2 modules program:
>
> > 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 -
>
> - Pokaz 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?

David RF

2/11/2011 11:30:00 AM

0

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:
>
> > > Hi i have 2 modules program:
>
> > > 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 -
>
> > - Pokaz 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

Ralph Spitzner

2/11/2011 11:38:00 AM

0

Mateusz_madi wrote:
>
> 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?

no you simply declare it in say main.c int g;
initialize it (if compiler supports this) like
not writing int g; but int g=3;

then you 'recognize it as being there' in your other
'module'

extern int g; // cannot initialize at compile time

_then_ increment it it like say:

foo()
{
g++;
}

of course you have to call that function.

Are you coming from Delphi, Pascal or something ?

So, you can use any global from any function, but
your 'modules' get alive when main() is called, hence
you have no 'modules' that can have their own lives....

This was not very technical :-P

But I think you have a general misperception of how that
executable is put together by the compiler/linker here...

Pls don't flame me for that :-)
-rasp

James Kuyper

2/11/2011 11:39:00 AM

0

On 02/11/2011 03:58 AM, Mateusz_madi wrote:
> Hi i have 2 modules program:
>
> 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 the initialization of 'a' to a value of 3 occurs before main()
starts executing. The assignment of the value of 8 to 'a' doesn't occur
until after main() starts executing. by the time main() calls druku, the
assignment is already complete.

When did you think the values of 3 and 8 would be given to 'a'?

--
James Kuyper

bert

2/11/2011 11:44:00 AM

0

On Feb 11, 9:51 am, 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:
>
> > > Hi i have 2 modules program:
>
> > > 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;
>
> 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?

Some dialects of C could have "extern int a" in
the module b.c, then it is clear that the "a"
which you overwrite in the second module is the
same global "a" as the one which you declared
and initialised in the first module.
--

pete

2/11/2011 12:41:00 PM

0

Mateusz_madi wrote:

> buth if i declare global let's say g in one module and would like to
> increment it in second module it is impossible

Let's say a.
You can do it this way:

/* BEGIN a.h */

void drukuj(void);

/* END a.h */


/* BEGIN a.c */

#include "a.h"

#include <stdio.h>

extern int a;

void
drukuj(void)
{
printf("%d\n", a);
++a;
}

/* END a.c */


/* BEGIN b.c */

#include "a.h"

int a;

int
main(void)
{
a = 8;
drukuj();
drukuj();
drukuj();
drukuj();
return 0;
}

/* END b.c */

--
pete

pete

2/11/2011 12:57:00 PM

0

pete wrote:
>
> Mateusz_madi wrote:
>
> > buth if i declare global let's say g in one module and would like to
> > increment it in second module it is impossible
>
> Let's say a.
> You can do it this way:
>
> /* BEGIN a.h */
>
> void drukuj(void);
>
> /* END a.h */
>
> /* BEGIN a.c */
>
> #include "a.h"
>
> #include <stdio.h>
>
> extern int a;
>
> void
> drukuj(void)
> {
> printf("%d\n", a);
> ++a;
> }
>
> /* END a.c */

Though I think it might have been better style
to have placed the extern declaration
inside of a.h instead of in a.c..

--
pete

Ben Bacarisse

2/11/2011 2:01:00 PM

0

Mateusz_madi <madi.czadi@gmail.com> writes:

> Hi i have 2 modules program:
>
> a.c
> ---------
> #include<stdio.h>
>
> int a=3;

This declares an identifier with external linkage and also defines it.

> void drukuj(){
> printf("%d", a);
> }
>
> -----------
> b.c
> -----------
>
> #include<stdio.h>
>
> int a;

This is called a tentative definition, because a similar definition may
come later that says some more about 'a';

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

But here, at the end of the translation unit, that tentative definition
becomes and real one, just as if you had written "int a = 0;".

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

Answers so far have missed a key point: the program is undefined.
Anything could happen. The C standard says "If an identifier declared
with external linkage is used [...] somewhere in the entire program
there shall be exactly one external definition for the identifier".

--
Ben.

Ben Bacarisse

2/11/2011 2:12:00 PM

0

bert <bert.hutchings@btinternet.com> writes:

> On Feb 11, 9:51 am, 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;
>>
>> 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?
>
> Some dialects of C could have "extern int a" in
> the module b.c,

I think this is a little misleading. What do you mean by "some
dialects"? What you have proposed is standard C. It's been that way
since the very first days of C -- from pre-ANSI C right up to the
current proposed draft of the next standard. I'd argue that any dialect
that did not permit "extern int a;" to mean what you want would have
little right to be called C.

> then it is clear that the "a"
> which you overwrite in the second module is the
> same global "a" as the one which you declared
> and initialised in the first module.

I don't think "overwrite" is the best word here. The extern declaration
simply refers to the same 'a' that was defined in the first translation
unit. (C does not have "modules" but I don't think that matters -- it's
a useful and well-known term that does cause any serious confusion. C
does not really have globals either -- and in this particular case, it
does matter because it is exactly the source of the OP's confusion.)

--
Ben.