Re: Need help in Global variable in c
- From: jacob navia <jacob@xxxxxxxxxx>
- Date: Sun, 30 Sep 2007 18:41:36 +0200
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
.
- References:
- Need help in Global variable in c
- From: yinglcs@xxxxxxxxx
- Need help in Global variable in c
- Prev by Date: Re: C program Questions
- Next by Date: Re: Need help in Global variable in c
- Previous by thread: Re: Need help in Global variable in c
- Next by thread: Need help reading file data
- Index(es):
Relevant Pages
|