Re: Need help in Global variable in c



"Mike Wahler" <mkwahler@xxxxxxxxxxxx> writes:

<yinglcs@xxxxxxxxx> wrote in message
news:1191169232.898864.162910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

I read this article about global variable in c:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html

But I have a few questions
1. how can I declare the global variable so that it can be set/get by
other files?

/* glob.c */
int global = 99; /* defines 'global' */

/* glob.h */
extern int global; /* declares that 'global' */
/* exists elsewhere */


/* file1.c */
#include <stdio.h>
#include "glob.h"

int main()
{
global = 42;
printf("%d\n", global);
return 0;
}

In the interest of error prevention and maintainability,
you should avoid globals if possible.

2. If I have multiple instance of the same program running in the same
machine (linux), will each program instance has its own of global
variable?

That depends entirely upon your platform (and possibly compiler
settings). The C language does not specify program behavior in
a multi-process environment.

There is a platform where different instances share the same
"globals" as demonstrated above?

To the OP : Multiple instances all have their own data unless some
platform specific sharing mechanism is used. The code above is process
private in the real world.

I would be seriously interested to hear of cases where is not the case
as I have never seen it before.


-Mike
.



Relevant Pages

  • Re: Need help in Global variable in c
    ... you should avoid globals if possible. ... That depends entirely upon your platform (and possibly compiler ... assurance that answers about such things posted here are correct. ... You'll have a far better chance of getting accurate information about ...
    (comp.lang.c)
  • Re: Need help in Global variable in c
    ... you should avoid globals if possible. ... That depends entirely upon your platform (and possibly compiler ... The C language does not specify program behavior in ... You'll have a far better chance of getting accurate information about ...
    (comp.lang.c)
  • Re: Changing from C thought to Perl
    ... AQ> difficult to bypass using global vars, and in fact global vars make ... modules which use globals for certain kinds of storage will not ... >> on every platform. ... AQ> Care to give an example where a Perl regexp works differently on two ...
    (comp.lang.perl.misc)
  • Re: Need help in Global variable in c
    ... how can I declare the global variable so that it can be set/get by ... you should avoid globals if possible. ... settings). ... The C language does not specify program behavior in ...
    (comp.lang.c)
  • RE: scope of public variables
    ... "Jim Thomlinson" wrote: ... If your globals are declared anywhere other than a standard code module ... is where did you declare your variable. ... Sub optCkforDupes_Click ...
    (microsoft.public.excel.programming)