Re: Need help in Global variable in c



yinglcs@xxxxxxxxx wrote:
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?

1) You declare the variable in one file. By default variables are
visible in other files
File f1.c

int n;

In other files you just use them declaring them extern:

File f2.c

extern int n;

....
n = 67;


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?


If you have different processes, they all will have different
versions of the variable since they run in different address spaces.

If you have several THREADS, that's another question. In each thread the
variable will be seen by all threads and can be modified by all
threads.

Note that when you fork(), the child process is a *process* i.e.
the address space is differeent, and the variable will receive the value
it had before the fork() but afterwards both process will be decoupled
and each one will NOT see the modifications of the other.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
.



Relevant Pages

  • Re: extern char and include practices
    ... > Alright, i have some questions concerning include files en global ... The include file is used to specify the access allowed to the code ... > a global in main and then use extern int lines;? ... declare it as local to the logfunction. ...
    (comp.lang.c)
  • Re: fork process to handle fifo input
    ... Unless you need backward compatabiliy you should probably "use ... warnings" rather than have a -w in the shebang. ... Always declare all variables as lexcally scoped in the smallest ... In Perl fork() does not return a negative number on ...
    (comp.lang.perl.misc)
  • Re: Glass Panel Longevity
    ... nearly every project that I work on, at least one of the machines not only does not have a C++ compiler for it, it doesn't have one that is POSIX or ANSI compliant either... ... Occasionally, you will see a developer declare a variable in a local block of code, but it doesn't seem to happen that often, primarily in some sort of loop counter or accumulator... ... Code should be readable and documentation should be inline so that you can remember *why* you were doing something a particular way when you have to come back in a couple of years and modify the code... ... extern int MYOBJ_x; ...
    (rec.aviation.piloting)
  • Re: Nice comments on Suse and MS
    ... equivalent amount of work into the fork or see it stagnate. ... some people declared the movement dead. ... So they had the same right and impact to declare it ... It reminds me of the punk era where people became punk by looking ...
    (alt.os.linux.suse)
  • Re: export global variables in DLL
    ... I declared it in the dll- header file like ... __declspecextern int IX; ... After including the header-file in the exe I compiled the program ... > how I have to declare global variables in a dll for exporting? ...
    (microsoft.public.vc.mfc)